dsflxcfuw27742248 2013-04-02 23:15
浏览 43
已采纳

PHP数组没有正确格式化

I can't seem to figure out what the problem is, but my PHP array isn't outputting as proper JSON. I removed the json_encode function, only to find out that my array's are cascading and not properly formatting themselves. Here is an example of the output (with json_encode removed):

Array (
    [0] => Array (
        [username] => g4m3b0y
        [profile_photo] => default.png
        [email] => michael@me.com
        [first_name] => Michael
        [last_name] => Simpson
    )
) 
Array (
    [0] => Array (
        [username] => g4m3b0y
        [profile_photo] => default.png
        [email] => michael@me.com
        [first_name] => Michael
        [last_name] => Simpson
    )
    [1] => Array (
        [username] => michelle
        [profile_photo] => default.png
        [email] => michelle@gmail.com
        [first_name] => Michelle
        [last_name] => Houston
    )
)

As you can see, it's creating duplicate results in separate arrays, hence my issue with the JSON output. Here is my code which is generating the results:

    $sql2 = "SELECT DISTINCT username, profile_photo, first_name, last_name, email FROM users WHERE (
        first_name LIKE '%$q%'
        OR last_name LIKE '%$q%'
    ) ORDER BY last_name";

    $sq2 = mysql_query($sql2);
    $st2 = mysql_num_rows($sq2);

    if($st2>=1) {   
        while($a = mysql_fetch_array($sq2)) {
            $userRow[] = array(
                'username' => $a['username'],
                'profile_photo'   => $a['profile_photo'],
                'email'   => $a['email'],
                'first_name'   => $a['first_name'],
                'last_name'   => $a['last_name']
            );
        header('Content-Type: application/json');
        echo json_encode($userRow);
        }
    }

    else {
        exit;
    }
  • 写回答

1条回答 默认 最新

  • doumeng4400 2013-04-02 23:23
    关注

    Your call to header and json_encode is inside your while loop. Move it outside and it'll compile the lot into one json string. At present it is outputting multiple separate ones.

    This also explains why you're getting the duplicate too. Iteration one adds the first row to the array $userRow, it then outputs the result which has got the key 0. Then the second iteration adds the next row to the existing array, and therefore gets the key 1. Then it echos the json encoded string again, therefore printing 0 and 1 this time. Hence 0 then 0, then 1

    $sql2 = "SELECT DISTINCT username, profile_photo, first_name, last_name, email FROM users WHERE (
        first_name LIKE '%$q%'
        OR last_name LIKE '%$q%'
    ) ORDER BY last_name";
    
    $sq2 = mysql_query($sql2);
    $st2 = mysql_num_rows($sq2);
    
    if($st2>=1) {   
        while($a = mysql_fetch_array($sq2)) {
            $userRow[] = array(
                'username' => $a['username'],
                'profile_photo'   => $a['profile_photo'],
                'email'   => $a['email'],
                'first_name'   => $a['first_name'],
                'last_name'   => $a['last_name']
            );
        }
    
        // I moved this below the above }
        header('Content-Type: application/json');
        echo json_encode($userRow);
    }
    
    else {
        exit;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。