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 :)