note 9037 added to array_merge_recursive
| From: | kc at hireability dot com | Date: | Thu, 12 Oct 2000 21:08:12 +0000 |
| Subject: | note 9037 added to array_merge_recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-6417@lists.php.net to get a copy of this message | ||
There seemed to be no built in function that would merge two arrays recursively and clobber any
existing key/value pairs. Array_Merge() is not recursive, and array_merge_recursive seemed to give
unsatisfactory results... it would append duplicate key/values.
So here's a cross between array_merge and array_merge_recursive
---------------------------------
function array_merge_clobber($a1,$a2) {
if(!is_array($a1) || !is_array($a2)) return false;
$newarray = $a1;
while (list($key, $val) = each($a2)) {
if (is_array($val) && $newarray[$key]) {
$newarray[$key] = array_merge_clobber($newarray[$key], $val);
} else {
$newarray[$key] = $val;
}
}
return $newarray;
}
-------------------------------------
http://www.php.net/manual/function.array-merge-recursive.php