简体   繁体   中英

Two array merge or push for JSON

I have two PHP array like below,

$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,

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,

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

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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