note 73843 added to function.array-merge-recursive
| From: | paha at paha dot hu | Date: | Mon, 12 Mar 2007 20:02:31 +0000 |
| Subject: | note 73843 added to function.array-merge-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124387@lists.php.net to get a copy of this message | ||
In this version the values are overwritten only if they are not an array. If the value is an array,
its elements will be merged/overwritten:
// array_merge_recursive which override value with next value.
// based on: http://www.php.net/manual/hu/function.array-merge-recursive.php
09-Dec-2006 03:38
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]);
}
}
if(count($args) > 2) {
// put the recursion
$result[$key] = call_user_func_array(__FUNCTION__, $args);
} else {
foreach($value as $vkey => $vval) {
$result[$key][$vkey] = $vval;
}
}
} else {
// simply put the value
$result[$key] = $value;
}
}
}
}
return $result;
}
----
Server IP: 69.147.83.197
Probable Submitter: 195.70.35.155 (proxied: 127.0.0.1)
----
Manual Page -- http://www.php.net/manual/en/function.array-merge-recursive.php
Edit -- https://master.php.net/note/edit/73843
Del: integrated -- https://master.php.net/note/delete/73843/integrated
Del: useless -- https://master.php.net/note/delete/73843/useless
Del: bad code -- https://master.php.net/note/delete/73843/bad+code
Del: spam -- https://master.php.net/note/delete/73843/spam
Del: non-english -- https://master.php.net/note/delete/73843/non-english
Del: in docs -- https://master.php.net/note/delete/73843/in+docs
Del: other reasons-- https://master.php.net/note/delete/73843
Reject -- https://master.php.net/note/reject/73843
Search -- https://master.php.net/manage/user-notes.php