PHP array_merge_recursive()函數 (PHP array_merge_recursive() function)
array_merge_recursive() function is used to merge two or more arrays, it returns a new array with merged elements. The only difference between array_merge() and array_merge_recursive() function is: "It merges the values having the same key as an array instead of overriding the values."
array_merge_recursive()函數用于合并兩個或多個數組,它返回一個具有合并元素的新數組。 “ array_merge()和array_merge_recursive()函數之間的唯一區別是: “它合并與數組具有相同鍵的值,而不是覆蓋這些值。”
Syntax:
句法:
array_merge_recursive(array1, array2,...);
It accepts two or more arrays and returns a new array with merged elements.
它接受兩個或多個數組,并返回帶有合并元素的新數組。
Examples:
例子:
Input:
$arr1 = array("a" => "Hello", "b" => "Hi");
$arr2 = array("a" => "Okay!", "d" => "Nothing");
Output:
Array
(
[a] => Array
(
[0] => Hello
[1] => Okay!
)
[b] => Hi
[d] => Nothing
)
PHP code:
PHP代碼:
<?php
$arr1 = array("a" => "Hello", "b" => "Hi");
$arr2 = array("a" => "Okay!", "d" => "Nothing");
//merging arrays
$arr3 = array_merge_recursive($arr1, $arr2);
//printing
print_r ($arr3);
?>
Output
輸出量
Array
(
[a] => Array
(
[0] => Hello
[1] => Okay!
)
[b] => Hi
[d] => Nothing
)
翻譯自: https://www.includehelp.com/php/array_merge_recursive-function-with-example.aspx