我有两个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但没有帮助,
任何帮助表示赞赏,
由于
答案 0 :(得分:1)
我假设数组具有相同的长度。
我不知道你的尝试方式,但你可以这样做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.