note 81409 added to function.array-merge-recursive

From: Date: Tue, 26 Feb 2008 19:23:46 +0000
Subject: note 81409 added to function.array-merge-recursive
Groups: php.notes 
Request: Send a blank email to php-notes+get-136508@lists.php.net to get a copy of this message
If you desire correct and performant behaviour (in contrast to the other postings) use this code. It works as documented above. If you want that keys are always perserved and not appended or renumbered if they are numeric comment out the "if (((string) $key) === ((string) intval($key)))" case. @spambegone at cratemedia dot com: using empty is not right way to check if an item is in the array use isset! <?php function array_merge_recursive2($array1, $array2) { $arrays = func_get_args(); $narrays = count($arrays); // check arguments // comment out if more performance is necessary (in this case the foreach loop will trigger a warning if the argument is not an array) for ($i = 0; $i < $narrays; $i ++) { if (!is_array($arrays[$i])) { // also array_merge_recursive returns nothing in this case trigger_error('Argument #' . ($i+1) . ' is not an array - trying to merge array with scalar! Returning null!', E_USER_WARNING); return; } } // the first array is in the output set in every case $ret = $arrays[0]; // merege $ret with the remaining arrays for ($i = 1; $i < $narrays; $i ++) { foreach ($arrays[$i] as $key => $value) { if (((string) $key) === ((string) intval($key))) { // integer or string as integer key - append $ret[] = $value; } else { // string key - megre if (is_array($value) && isset($ret[$key])) { // if $ret[$key] is not an array you try to merge an scalar value with an array - the result is not defined (incompatible arrays) // in this case the call will trigger an E_USER_WARNING and the $ret[$key] will be null. $ret[$key] = array_merge_recursive2($ret[$key], $value); } else { $ret[$key] = $value; } } } } return $ret; } // Examples: print_r(array_merge_recursive2(array('A','B','C' => array(1,2,3)), array('D','C' => array(1,4)))); /* Array ( [0] => A [1] => B [C] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 1 [4] => 4 ) [2] => D )*/ print_r(array_merge_recursive2(array('A','B','0' => array(1,2,3)), array('D','0' => array(1,array(4))))); /* Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [1] => B [2] => Array ( [0] => 1 [1] => Array ( [0] => 4 ) ) )*/ print_r(array_merge_recursive2(array('A' => array('A' => 1)), array('A' => array('A' => array(2))))); /* Warning: Argument #1 is not an array - trying to merge array with scalar! Returning null! in ... on line ... */ var_dump(array_merge_recursive2(array('A' => array('A' => 2)), array('A' => array('A' => null)))); /* array(1) { ["A"]=> array(1) { ["A"]=> NULL } } */ ?> ---- Server IP: 217.160.72.57 Probable Submitter: 217.68.170.200 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge-recursive.php Edit -- https://master.php.net/note/edit/81409 Del: integrated -- https://master.php.net/note/delete/81409/integrated Del: useless -- https://master.php.net/note/delete/81409/useless Del: bad code -- https://master.php.net/note/delete/81409/bad+code Del: spam -- https://master.php.net/note/delete/81409/spam Del: non-english -- https://master.php.net/note/delete/81409/non-english Del: in docs -- https://master.php.net/note/delete/81409/in+docs Del: other reasons-- https://master.php.net/note/delete/81409 Reject -- https://master.php.net/note/reject/81409 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136508) next »