note 34082 deleted from function.array-merge-recursive by sniper
| From: | sniper@php.net | Date: | Mon, 06 Oct 2003 22:58:56 +0000 |
| Subject: | note 34082 deleted from function.array-merge-recursive by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57934@lists.php.net to get a copy of this message | ||
Note Submitter: bishop
----
Pass-by-reference and varargs version of kc at hireability dot com array_merge_clobber() function:
function array_merge_clobber(&$a1 /* ... */) {
if (! is_array($a1)) {
trigger_error('First argument to array_merge_clobber() must be an array',
E_USER_ERROR);
return false;
}
$argc = func_num_args();
for ($i = 1; $i < $argc; $i++) {
$a2 = func_get_arg($i);
if (! is_array($a2)) {
trigger_error('Argument #' . ($i+1) . ' to array_merge_clobber() is not
an array; not merged', E_USER_WARNING);
continue;
}
foreach ($a2 as $key => $val) {
if (is_array($val) && is_array($a1[$key])) {
array_merge_clobber($a1[$key], $val);
} else {
$a1[$key] = $val;
}
}
}
return true;
}
Not a drop in replacement (per se) for array_merge_recursive(), but it's got its benefits.
Call thusly:
$rc = array_merge_clobber($a1, $a2 /* , .... */);
if ($rc) {
echo 'a2 fed into a1 okay';
} else {
echo 'something went awry';
}