note 71646 deleted from function.array-merge-recursive by felipe
| From: | felipe@php.net | Date: | Tue, 11 Dec 2007 10:05:40 +0000 |
| Subject: | note 71646 deleted from function.array-merge-recursive by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133354@lists.php.net to get a copy of this message | ||
Note Submitter: bersace03 at laposte dot net
----
Hi all,
I wrote a array_merge_recursive_unique which do exactly like array_merge_recursive2 but allow
infinity of arrays as argument.
<?php
// array_merge_recursive which override value with next value.
function array_merge_recursive_unique ($array0, $array1)
{
$arrays = func_get_args ();
$remains = $arrays;
// We walk through each arrays and put value in the results (without
// considering previous value).
$result = array ();
// loop available array
foreach ($arrays as $array) {
// The first remaining array is $array. We are processing it. So
// we remove it from remaing arrays.
array_shift ($remains);
// We don't care non array param, like array_merge since PHP 5.0.
if (is_array ($array)) {
// Loop values
foreach ($array as $key => $value) {
if (is_array ($value)) {
// we gather all remaining arrays that have such key available
$args = array ();
foreach ($remains as $remain) {
if (array_key_exists ($key, $remain)) {
array_push ($args, $remain[$key]);
}
}
// put the recursion
$result[$key] = call_user_func_array (__FUNCTION__, $args);
}
else {
// simply put the value
$result[$key] = $value;
}
}
}
}
return $result;
}
?>
Enjoy :)