Merge one or more arrays recursively
If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.
Syntax
array_merge_recursive ([ array $... ] ) : array
Parameters
...
Variable list of arrays to recursively merge.
Return
An array of values resulted from merging the arguments together. If called without any arguments, returns an empty array.
Examples
1 · void
<? $return = array_merge_recursive(); print_r($return); ?>
Array ( )
2 · Array · Indexed
<? $array = array(0, 1); $return = array_merge_recursive($array); print_r($return); ?>
Array ( [0] => 0 [1] => 1 )
3 · Array · Associative
<? $array = array("a" => 0, "b" => 1); $return = array_merge_recursive($array); print_r($return); ?>
Array ( [a] => 0 [b] => 1 )
4 · ... · Indexed
<? $array1 = array(0, 1); $array2 = array(2, 3); $return = array_merge_recursive($array1, $array2); print_r($return); ?>
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )
5 · ... · Associative
<? $array1 = array("a" => 0, "b" => 1); $array2 = array("c" => 2, "d" => 3); $return = array_merge_recursive($array1, $array2); print_r($return); ?>
Array ( [a] => 0 [b] => 1 [c] => 2 [d] => 3 )
6 · Same Key · Indexed
<? $array1 = array(0, 1); $array2 = array(0 => 2, 1 => 3); $return = array_merge_recursive($array1, $array2); print_r($return); ?>
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )
7 · Same Key · Associative
<? $array1 = array("a" => 0, "b" => 1); $array2 = array("a" => 2, "b" => 3); $return = array_merge_recursive($array1, $array2); print_r($return); ?>
Array ( [a] => Array ( [0] => 0 [1] => 2 ) [b] => Array ( [0] => 1 [1] => 3 ) )