note 104145 added to function.array-merge-recursive

From: Date: Thu, 26 May 2011 05:23:22 +0000
Subject: note 104145 added to function.array-merge-recursive
Groups: php.notes 
Request: Send a blank email to php-notes+get-180050@lists.php.net to get a copy of this message
There are a lot of examples here for recursion that are meant to behave more like array_merge() but they don't get it quite right or are fairly customised. I think this version is most similar, takes more than 2 arguments and can be renamed in one place: <?php function array_merge_recursive_simple() { if (func_num_args() < 2) { trigger_error(__FUNCTION__ .' needs two or more array arguments', E_USER_WARNING); return; } $arrays = func_get_args(); $merged = array(); while ($arrays) { $array = array_shift($arrays); if (!is_array($array)) { trigger_error(__FUNCTION__ .' encountered a non array argument', E_USER_WARNING); return; } if (!$array) continue; foreach ($array as $key => $value) if (is_string($key)) if (is_array($value) && array_key_exists($key, $merged) && is_array($merged[$key])) $merged[$key] = call_user_func(__FUNCTION__, $merged[$key], $value); else $merged[$key] = $value; else $merged[] = $value; } return $merged; } $a1 = array( 88 => 1, 'foo' => 2, 'bar' => array(4), 'x' => 5, 'z' => array( 6, 'm' => 'hi', ), ); $a2 = array( 99 => 7, 'foo' => array(8), 'bar' => 9, 'y' => 10, 'z' => array( 'm' => 'bye', 11, ), ); $a3 = array( 'z' => array( 'm' => 'ciao', ), ); var_dump(array_merge($a1, $a2, $a3)); var_dump(array_merge_recursive_simple($a1, $a2, $a3)); var_dump(array_merge_recursive($a1, $a2, $a3)); ?> gives: array(7) { array(7) { array(7) { int(1) int(1) int(1) ["foo"]=> ["foo"]=> ["foo"]=> array(1) { array(1) { array(2) { [0]=> [0]=> [0]=> int(8) int(8) int(2) } } [1]=> ["bar"]=> ["bar"]=> int(8) int(9) int(9) } ["x"]=> ["x"]=> ["bar"]=> int(5) int(5) array(2) { ["z"]=> ["z"]=> [0]=> array(1) { array(3) { int(4) ["m"]=> [0]=> [1]=> string(4) "ciao" int(6) int(9) } ["m"]=> } [1]=> string(4) "ciao" ["x"]=> int(7) [1]=> int(5) ["y"]=> int(11) ["z"]=> int(10) } array(3) { } [1]=> [0]=> int(7) int(6) ["y"]=> ["m"]=> int(10) array(3) { } [0]=> string(2) "hi" [1]=> string(3) "bye" [2]=> string(4) "ciao" } [1]=> int(11) } [1]=> int(7) ["y"]=> int(10) } ---- Server IP: 117.55.229.1 Probable Submitter: 219.90.138.207 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge-recursive.php Edit -- https://master.php.net/note/edit/104145 Del: integrated -- https://master.php.net/note/delete/104145/integrated Del: useless -- https://master.php.net/note/delete/104145/useless Del: bad code -- https://master.php.net/note/delete/104145/bad+code Del: spam -- https://master.php.net/note/delete/104145/spam Del: non-english -- https://master.php.net/note/delete/104145/non-english Del: in docs -- https://master.php.net/note/delete/104145/in+docs Del: other reasons-- https://master.php.net/note/delete/104145 Reject -- https://master.php.net/note/reject/104145 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#180050) next »