This function didn't work for me - or it didn't do what I thought it would. So I wrote the below function, which merges two arrays, and returns the resulting array. The base array is the left one ($a1), and if a key is set in both arrays, the right value has precedence. If a value in the left one is an array and also an array in the right one, the function calls itself (recursion). If the left one is an array and the right one exists but is not an array, then the right non-array-value will be used.
*Any key only appearing in the right one will be ignored*
- as I didn't need values appearing only in the right in my implementation, but if you want that you could make some fast fix.
function array_merge_recursive_leftsource(&$a1, &$a2) {
$newArray = array();
foreach ($a1 as $key => $v) {
if (!isset($a2[$key])) {
$newArray[$key] = $v;
continue;
}
if (is_array($v)) {
if (!is_array($a2[$key])) {
$newArray[$key] = $a2[$key];
continue;
}
$newArray[$key] = array_merge_recursive_leftsource($a1[$key], $a2[$key]);
continue;
}
$newArray[$key] = $a2[$key];
}
return $newArray;
}