note 54945 deleted from function.array-merge by nlopess

From: Date: Wed, 20 Jul 2005 12:59:49 +0000
Subject: note 54945 deleted from function.array-merge by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-92212@lists.php.net to get a copy of this message
Note Submitter: php at moechofe dot com ---- <?php /* * array_deep_merge * * array array_deep_merge ( array array1 [, array array2 [, array ...]] ) * * Like array_merge * * array_deep_merge() merges the elements of one or more arrays together so * that the values of one are appended to the end of the previous one. It * returns the resulting array. * If the input arrays have the same string keys, then the later value for * that key will overwrite the previous one. If, however, the arrays contain * numeric keys, the later value will not overwrite the original value, but * will be appended. * If only one array is given and the array is numerically indexed, the keys * get reindexed in a continuous way. * * Different from array_merge * If string keys have arrays for values, these arrays will merge recursively. */ 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; } } /* * test */ $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>"; ?>

« previous php.notes (#92212) next »