可以使用array_merge_recursive()函数来合并多维数组中相同的键。
示例代码如下:
$array1 = array(
"color" => array("favorite" => "red"),
"food" => array("fruit" => "apple", "vegetable" => "carrot")
);
$array2 = array(
"color" => array("favorite" => "blue"),
"food" => array("fruit" => "banana")
);
$result = array_merge_recursive($array1, $array2);
print_r($result);
输出结果为:
Array
(
[color] => Array
(
[favorite] => Array
(
[0] => red
[1] => blue
)
)
[food] => Array
(
[fruit] => Array
(
[0] => apple
[1] => banana
)
[vegetable] => carrot
)
)
在合并过程中,相同的键会被合并为一个数组,然后是其他键值对的合并。