note 87518 added to function.array-merge-recursive
| From: | phil dot kemmeter at gmail dot com | Date: | Mon, 08 Dec 2008 18:45:57 +0000 |
| Subject: | note 87518 added to function.array-merge-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-147920@lists.php.net to get a copy of this message | ||
I've edit this version even a little bit more, so that the function does not override any
values, but inserts them at a free key in the array:
function my_array_merge ($arr,$ins) {
if(is_array($arr))
{
if(is_array($ins)) foreach($ins as $k=>$v)
{
if(isset($arr[$k])&&is_array($v)&&is_array($arr[$k]))
{
$arr[$k] = my_array_merge($arr[$k],$v);
}
else {
// This is the new loop :)
while (isset($arr[$k]))
$k++;
$arr[$k] = $v;
}
}
}
elseif(!is_array($arr)&&(strlen($arr)==0||$arr==0))
{
$arr=$ins;
}
return($arr);
}
Example:
$array1 = array(
100 => array(30),
200 => array(20, 30)
);
$array2 = array(
100 => array(40),
201 => array(60, 30)
);
print_r(my_array_merge($array1,$array2));
Output with array_merge_recursive:
Array
(
[0] => Array
(
[0] => 30
)
[1] => Array
(
[0] => 20
[1] => 30
)
[2] => Array
(
[0] => 40
)
)
This is not the result, I expect from a MERGE-Routine...
Output with the current function:
Array
(
[100] => Array
(
[0] => 30
[1] => 40
)
[200] => Array
(
[0] => 20
[1] => 30
)
)
This is what I want :)
----
Server IP: 217.160.72.57
Probable Submitter: 88.217.84.82
----
Manual Page -- http://www.php.net/manual/en/function.array-merge-recursive.php
Edit -- https://master.php.net/note/edit/87518
Del: integrated -- https://master.php.net/note/delete/87518/integrated
Del: useless -- https://master.php.net/note/delete/87518/useless
Del: bad code -- https://master.php.net/note/delete/87518/bad+code
Del: spam -- https://master.php.net/note/delete/87518/spam
Del: non-english -- https://master.php.net/note/delete/87518/non-english
Del: in docs -- https://master.php.net/note/delete/87518/in+docs
Del: other reasons-- https://master.php.net/note/delete/87518
Reject -- https://master.php.net/note/reject/87518
Search -- https://master.php.net/manage/user-notes.php