簡體   English   中英

如何合並兩個多維數組

[英]How to merge two multidimensional arrays

我有兩個數組:

$arr1 = array(
    'attributes' => array(
        'fruit'     => 'banana', 
    ),
);

$arr2 = array(
    'attributes' => array(
        'color'    => 'red', 
    ),
);

$result = array_merge($arr1, $arr2);

結果是:

Array ( [attributes] => Array ( [color] => red ) ) 

但是我的預期結果是:

Array ( [attributes] => Array ( [color] => red [fruit] => banana ) ) 

我做錯了什么? 我應該使用array_merge還是僅使用array_push並僅使用('color'=>'red')會更好更好些?

array_merge_recursive()非常適合這里。

$resultArray = array_merge_recursive($arr1, $arr2);

嘗試這個:

$result = array('attributes' => array_merge($arr1['attributes'], $arr2['attributes']));
print_r($result);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM