簡體   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