There are a lot of examples here for recursion that are meant to behave more like array_merge() but they don't get it quite right or are fairly customised. I think this version is most similar, takes more than 2 arguments and can be renamed in one place:
<?php
function array_merge_recursive_simple() {
if (func_num_args() < 2) {
trigger_error(__FUNCTION__ .' needs two or more array arguments', E_USER_WARNING);
return;
}
$arrays = func_get_args();
$merged = array();
while ($arrays) {
$array = array_shift($arrays);
if (!is_array($array)) {
trigger_error(__FUNCTION__ .' encountered a non array argument', E_USER_WARNING);
return;
}
if (!$array)
continue;
foreach ($array as $key => $value)
if (is_string($key))
if (is_array($value) && array_key_exists($key, $merged) && is_array($merged[$key]))
$merged[$key] = call_user_func(__FUNCTION__, $merged[$key], $value);
else
$merged[$key] = $value;
else
$merged[] = $value;
}
return $merged;
}
$a1 = array(
88 => 1,
'foo' => 2,
'bar' => array(4),
'x' => 5,
'z' => array(
6,
'm' => 'hi',
),
);
$a2 = array(
99 => 7,
'foo' => array(8),
'bar' => 9,
'y' => 10,
'z' => array(
'm' => 'bye',
11,
),
);
$a3 = array(
'z' => array(
'm' => 'ciao',
),
);
var_dump(array_merge($a1, $a2, $a3));
var_dump(array_merge_recursive_simple($a1, $a2, $a3));
var_dump(array_merge_recursive($a1, $a2, $a3));
?>
gives:
array(7) { array(7) { array(7) {
int(1) int(1) int(1)
["foo"]=> ["foo"]=> ["foo"]=>
array(1) { array(1) { array(2) {
[0]=> [0]=> [0]=>
int(8) int(8) int(2)
} } [1]=>
["bar"]=> ["bar"]=> int(8)
int(9) int(9) }
["x"]=> ["x"]=> ["bar"]=>
int(5) int(5) array(2) {
["z"]=> ["z"]=> [0]=>
array(1) { array(3) { int(4)
["m"]=> [0]=> [1]=>
string(4) "ciao" int(6) int(9)
} ["m"]=> }
[1]=> string(4) "ciao" ["x"]=>
int(7) [1]=> int(5)
["y"]=> int(11) ["z"]=>
int(10) } array(3) {
} [1]=> [0]=>
int(7) int(6)
["y"]=> ["m"]=>
int(10) array(3) {
} [0]=>
string(2) "hi"
[1]=>
string(3) "bye"
[2]=>
string(4) "ciao"
}
[1]=>
int(11)
}
[1]=>
int(7)
["y"]=>
int(10)
}