Skip to content

Instantly share code, notes, and snippets.

@antic183
Last active June 2, 2016 14:28
Show Gist options
  • Save antic183/aed43422be00aada8964021f01fce79b to your computer and use it in GitHub Desktop.
Save antic183/aed43422be00aada8964021f01fce79b to your computer and use it in GitHub Desktop.
<?php
$arr1 = array(
"a" => ["x", "d" => "override me..", "d2" => ["q"], "v" => "NOT-IN-B"],
"b" => ["d" => 155, "fff" => ["aa"=>551]],
5, 8, 9
);
$arr2 = array(
"a" => ["v", "d" => "overrided!", "qq" => "ONLY-IN-B"],
"b" => ["xxx" => 155, "fff" => ["aa"=>552]],
5, 7
);
function merge_array_recursive($a, $b) {
$new =& $b;
function deepMerge($a, &$new) {
foreach($a as $kA1 => $vA1) {
if (!isset($new[$kA1])) {
$new[$kA1] = $vA1;
}
if (is_array($vA1) && is_array($new[$kA1])) {
deepMerge($vA1, $new[$kA1]);
}
}
}
deepMerge($a, $b);
return $new;
}
$res = merge_array_recursive($arr1, $arr2);
echo '<pre>';
print_r($res);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment