note 92985 deleted from function.array-merge-recursive by danbrown
| From: | danbrown@php.net | Date: | Tue, 18 Aug 2009 17:21:34 +0000 |
| Subject: | note 92985 deleted from function.array-merge-recursive by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-159602@lists.php.net to get a copy of this message | ||
Note Submitter: dom111.co.uk
----
I thought I'd have a go at the array_merge_recursive_distinct() function myself.
Allows variable number of arguments!
<?php
/**
* array_merge_recursive_distinct
*
* Recursively process an array merge all child nodes together
*
* @return array
* @author Dom Hastings
*/
function array_merge_recursive_distinct() {
switch (func_num_args()) {
case 0:
return array();
break;
case 1:
return (array) func_get_arg(0);
break;
default:
$a = func_get_args();
$s = (array) array_shift($a);
foreach ($a as $i => $b) {
if (!is_array($b)) {
$b = (array) $b;
}
foreach ($b as $k => $v) {
if (is_numeric($k)) {
$s[] = $v;
} else {
if (isset($s[$k])) {
if (is_array($s[$k]) && is_array($v)) {
$s[$k] = array_merge_recursive_distinct($s[$k], $v);
} else {
$s[$k] = $v;
}
} else {
$s[$k] = $v;
}
}
}
}
break;
}
return $s;
}
?>