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

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
        )

)

所以这是我的代码,获取我需要的查询结果 p>

  $ json_array = array(); 
 $ sql =“call rep_info();”; 
 $ result = $ connect-> query($ sql); 
while($ row = $ result-&gt  ; fetch_assoc()){
 $ json_array [] = $ row; 
} 
 
mysqli_free_result($ result);  
mysqli_next_result($连接);  
 
 // Buat info user 
 $ json_array2 = array(); 
 $ sql =“call rep_user();”; 
 
 $ result = $ connect-> query($ sql); \  nwhile($ row = $ result-> fetch_assoc()){
 $ json_array2 [] [“user”] = $ row; 
} 
  code>  pre> 
 
 

然后我将两个数组合并为一个这样的 p>

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

将合并后的数组编码为 以及回显变量 p>

  $ encoded = json_encode($ timeline); 
 
echo $ encoded; 
  code>  pre> 
 
  
 
 

这段代码有什么问题? !谢谢 P>

编辑: p>

 的print_r($ json_array [0]); 
print_r($ json_array2 [0]); 
  
Array 
(
 [id] => 11087 
 [消息] =>帖子晚了。
Darmo ramai lancar 
 [纬度] => -7.292500019073486 
 [经度] => 112.7388916015625 
  [image] => 1529894809620.jpeg 
 [thumb] => 
 [sub_category] ​​=> 
 [is_need_respond] => 0 
 [is_sticky] => 0 
 [is_hidden] =&gt  ; 0 
 [已查看] => 18 
 [deleted_at] => 
 [created_at] => 2018-06-25 02:48:04 
)
Array 
(
 [用户]  =>数组
(
 [id] => 621 
 [name] => asd 
 [用户名] => asd 
 [已验证] => 2 
 [avatar] =&gt  ; icfab.jpg 
 [level] => 10 
)
 
)
  code>  pre> 
  div>

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.