繁体   English   中英

两个数组合并或推送JSON

[英]Two array merge or push for JSON

我有两个如下的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);

我想以这种方式合并这两个数组,使其应如下所示输出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}

我尝试使用array_merge,array_push但没有帮助,

任何帮助表示赞赏,

谢谢

我假设数组的长度相同。

我不知道您怎么尝试,但是您可以像这样使用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