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 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图