duan2428 2018-08-03 10:05
浏览 214
已采纳

Json_encode一个合并的数组,但只返回1行

So this is my code, fetching the result of the query i need

$json_array = array();
$sql = "call rep_info();";
$result = $connect->query($sql);
while ($row = $result->fetch_assoc()) {
  $json_array[] = $row;
}

mysqli_free_result($result);  
mysqli_next_result($connect); 

// Buat info user
$json_array2 = array();
$sql = "call rep_user();";

$result = $connect->query($sql);
while ($row = $result->fetch_assoc()) {
  $json_array2[]["user"] = $row;
}

Then i merged the two arrays into one like this

$timeline = array_merge($json_array[0],$json_array2[0]);
$timeline2 = array_merge($json_array[1],$json_array2[1]);

$timeline = array_merge($timeline,$timeline2);

Encoded the merged array as well as echoing the variable

$encoded = json_encode($timeline);

echo $encoded;

So basically what i want is displaying 2 rows of the query result which is from json_array[0] and json_array[1]. But i only get 1 row in the echo result which the json_array[1] and json_array2[1].

What is the problem in this code? Thanks!

EDIT:

print_r($json_array[0]);
print_r($json_array2[0]);

Array
(
    [id] => 11087
    [message] => Late post.
Darmo ramai lancar
    [latitude] => -7.292500019073486
    [longitude] => 112.7388916015625
    [image] => 1529894809620.jpeg
    [thumb] => 
    [sub_category] => 
    [is_need_respond] => 0
    [is_sticky] => 0
    [is_hidden] => 0
    [viewed] => 18
    [deleted_at] => 
    [created_at] => 2018-06-25 02:48:04
)
Array
(
    [user] => Array
        (
            [id] => 621
            [name] => asd
            [username] => asd
            [verified] => 2
            [avatar] => icfab.jpg
            [level] => 10
        )

)
  • 写回答

1条回答 默认 最新

  • dougu4027 2018-08-03 11:16
    关注

    Try like this

    $timeline = $json_array[0];
    $timeline['user'] = $json_array2[0]['user'];
    $timeline2 = $json_array[1];
    $timeline2['user'] = $json_array2[1]['user'];
    
    $timeline = [$timeline, $timeline2];
    
    $encoded = json_encode($timeline);
    
    echo $encoded;
    

    EDIT

    Assuming $json_array has the same length as $json_array2 you can merge them like this

    foreach($json_array as $idx => &$item)
    {
      $item['user'] = &$json_array2[$idx]['user'];
    }
    echo json_encode($json_array);
    

    UPDATE

    I am not sure why you do not understand this 1-line cycle. I will try to explain it as good as I can.

    You have 2 arrays of the same length - $json_array and $json_array2. You iterate over each element in the $json_array and look at the same array index in $json_array2. When you have the corresponding elements from both arrays - you simply copy the user key from the second array into the first one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题