Home >Backend Development >PHP Tutorial >php-Arrays function-array_merge_recursive-recursively merge one or more arrays_PHP tutorial
array_merge_recursive() recursively merges one or more arrays
【Function】
This function combines the elements of one or more arrays. The values in one array are appended to the previous array.
Return the array as the result. If the input arrays have the same string key name, the values will be merged into one
In an array, this will go down recursively, so if a value is itself an array, the function will merge it according to the corresponding entry
is another array. However, if the array details have the same array key name, the latter value will not overwrite the original value, and
is appended to the back.
【Scope of use】
php4>4.0.1, php5.
【Use】
array array_merge_recursive( array array1[,array...] )
arrayn/required/array to be used for merging
【Example】
[php]
$arr1 = array("color"=>array("favorite"=>"red"),5);
$arr2 = array(10,"color"=>array("favorite"=>"green","blue"));
var_dump(array_merge_recursive($arr1,$arr2));
/*
array(3) {
["color"]=>
array(2) {
["favorite"]=>
array(2) {
[0]=>
string(3) "red"
[1]=>
string(5) "green"
}
[0]=>
String(4) "blue"
}
[0]=>
int(5)
[1]=>
int(10)
}
*/
Excerpted from zuodefeng’s notes