note 51121 added to function.array-merge

From: Date: Sun, 20 Mar 2005 23:54:31 +0000
Subject: note 51121 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-86724@lists.php.net to get a copy of this message
This is a simple function to merge an array multidimensional: <?php function multimerge ($array1, $array2) { if (is_array($array2) && count($array2)) { foreach ($array2 as $k => $v) { if (is_array($v) && count($v)) { $array1[$k] = multimerge($array1[$k], $v); } else { $array1[$k] = $v; } } } else { $array1 = $array2; } return $array1; } $array1 = array( 1 => "value1", 2 => "value2", 3 => array( "valor2" => "test3", ), "more" => array( "alot" => "A lot of values", ":)" => "Cont" ) ); $array2 = array( 2 => "other value", 3 => array( "valor3" => "more values", ), "more" => array( "alot" => "Change the value" ), "final" => "Final Value" ); print_r(multimerge($array1, $array2)); ?> The output is: Array ( [1] => value1 [2] => other value [3] => Array ( [valor2] => test3 [valor3] => more values ) [more] => Array ( [alot] => Change the value [:)] => Cont ) [final] => Final Value ) ---- Manual Page -- http://www.php.net/manual/en/function.array-merge.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+51121 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51121&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51121&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51121&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51121&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51121&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+51121&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#86724) next »