- var_dump: documentation ( source)
- array_replace_recursive: documentation ( source)
<?php
function arrayMergeRecursive($arraySource, $arrayDestination) {
$temp = array();
foreach ($arraySource as $key => $value) {
if (is_array($value)) {
$associatedDestination = array();
if (isset($arrayDestination[$key])) {
$associatedDestination = $arrayDestination[$key];
}
$temp[$key] = arrayMergeRecursive($value, $associatedDestination);
continue;
}
if (isset($arrayDestination[$key])) {
$temp[$key] = $arrayDestination[$key];
} else {
$temp[$key] = $arraySource[$key];
}
}
return $temp;
}
$source = ["a" => 1, "b" => 2, "c" => ["x" => 1, "y" => 2]];
$x = ["c" => 1, "d" => 10, "a" => 5];
var_dump(arrayMergeRecursive($source, $x));
var_dump(array_replace_recursive($source, $x));