简体   繁体   English

两个数组合并或推送JSON

[英]Two array merge or push for JSON

I have two PHP array like below, 我有两个如下的PHP数组,

$health_table1[]    = array(
            'department_name'   =>  $department['department_name'],
            'department_total'  =>  $department_total);


$health_table2[]    = array(
            'status_name'   =>  $status['status_name'],
            'status_total' => $status_total);

I want to merge this two array in such a way, that it should output JSON as below, 我想以这种方式合并这两个数组,使其应如下所示输出JSON,

0   
:
{department_name: "Iphone", department_total: 1, status_name: 'test1', status_total: 5}
1
:
{department_name: "Macbook", department_total: 2, status_name: 'test2', status_total: 7}
2
:
{department_name: "Training", department_total: 0, status_name: 'test3', status_total: 9}

I tried with array_merge, array_push but did not help, 我尝试使用array_merge,array_push但没有帮助,

Any help appreciated, 任何帮助表示赞赏,

Thanks 谢谢

I assume arrays have the same length. 我假设数组的长度相同。

I don't know how you try, but you can do with array_merge like this 我不知道您怎么尝试,但是您可以像这样使用array_merge

$return = [];
foreach($health_table1 as $key => $value){
   $return[] = array_merge($health_table1[$key], $health_table2[$key]);
}

echo json_encode($return);//<- here is the merged array.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM