<?php
function array_deep_merge()
{
switch( func_num_args() )
{
case 0 : return false; break;
case 1 : return func_get_arg(0); break;
case 2 :
$args = func_get_args();
$args[2] = array();
if( is_array($args[0]) and is_array($args[1]) )
{
foreach( array_unique(array_merge(array_keys($args[0]),array_keys($args[1]))) as $key )
if( is_string($key) and is_array($args[0][$key]) and is_array($args[1][$key]) )
$args[2][$key] = array_deep_merge( $args[0][$key], $args[1][$key] );
elseif( is_string($key) and isset($args[0][$key]) and isset($args[1][$key]) )
$args[2][$key] = $args[1][$key];
elseif( is_integer($key) and isset($args[0][$key]) and isset($args[1][$key]) ) {
$args[2][] = $args[0][$key]; $args[2][] = $args[1][$key]; }
elseif( is_integer($key) and isset($args[0][$key]) )
$args[2][] = $args[0][$key];
elseif( is_integer($key) and isset($args[1][$key]) )
$args[2][] = $args[1][$key];
elseif( ! isset($args[1][$key]) )
$args[2][$key] = $args[0][$key];
elseif( ! isset($args[0][$key]) )
$args[2][$key] = $args[1][$key];
return $args[2];
}
else return $args[1]; break;
default :
$args = func_get_args();
$args[1] = array_deep_merge( $args[0], $args[1] );
array_shift( $args );
return call_user_func_array( 'array_deep_merge', $args );
break;
}
}
$a = array(
0,
array( 0 ),
'integer' => 123,
'integer456_merge_with_integer444' => 456,
'integer789_merge_with_array777' => 789,
'array' => array( "string1", "string2" ),
'array45_merge_with_array6789' => array( "string4", "string5" ),
'arraykeyabc_merge_with_arraykeycd' => array( 'a' => "a", 'b' => "b", 'c' => "c" ),
'array0_merge_with_integer3' => array( 0 ),
'multiple_merge' => array( 1 ),
);
$b = array(
'integer456_merge_with_integer444' => 444,
'integer789_merge_with_array777' => array( 7,7,7 ),
'array45_merge_with_array6789' => array( "string6", "string7", "string8", "string9" ),
'arraykeyabc_merge_with_arraykeycd' => array( 'c' => "ccc", 'd' => "ddd" ),
'array0_merge_with_integer3' => 3,
'multiple_merge' => array( 2 ),
);
$c = array(
'multiple_merge' => array( 3 ),
);
echo "<pre>".htmlentities(print_r( array_deep_merge( $a, $b, $c ), true))."</pre>";
?>