note 48181 modified in function.array-walk-recursive by aidan

From: Date: Tue, 22 Feb 2005 07:07:20 +0000
Subject: note 48181 modified in function.array-walk-recursive by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-85359@lists.php.net to get a copy of this message
I wondered about an array_mergedown function working with PHP4, using an array_walk_recursive-like function which just merges any number of arrays of any dimension to a one dimension array containing every key=>value part of all arrays: <?php function array_mergedown() { global $outarray; $outarray = array(); function array_walk_recphp4(&$val,$key) { global $outarray; if (is_array($val)) array_walk($val,'array_walk_recphp4'); else { $outarray[$key] = $val; } } $params = func_get_args(); foreach ($params as $subarr) { array_walk_recphp4($subarr, ''); } return $outarray; } ?> For testing: <?php $arr1[]["foo1"] = "bar1"; $arr2["foo2"] = "bar2"; $arr2[12] = "bar3"; $arr2[10]["foo4"] = "bar4"; $arr2[]["foo4"][0]["foo5"] = "bar5"; $arr3 = "nono"; print_r(array_mergedown($arr1, $arr2, $arr3)); ?> returns: Array ( [foo1] => bar1 [foo2] => bar2 [12] => bar3 [foo4] => bar4 [foo5] => bar5 ) I hope this helped someone :) --was-- I wondered about an array_mergedown function working with PHP4, using an array_walk_recursive-like function which just merges any number of arrays of any dimension to a one dimension array containing every key=>value part of all arrays: //code start function array_mergedown() { global $outarray; $outarray = array(); function array_walk_recphp4(&$val,$key) { global $outarray; if (is_array($val)) array_walk($val,'array_walk_recphp4'); else { $outarray[$key] = $val; } } $params = func_get_args(); foreach ($params as $subarr) { array_walk_recphp4($subarr, ''); } return $outarray; } //code stop // for testing: $arr1[]["foo1"] = "bar1"; $arr2["foo2"] = "bar2"; $arr2[12] = "bar3"; $arr2[10]["foo4"] = "bar4"; $arr2[]["foo4"][0]["foo5"] = "bar5"; $arr3 = "nono"; print_r(array_mergedown($arr1, $arr2, $arr3)); // returns: Array ( [foo1] => bar1 [foo2] => bar2 [12] => bar3 [foo4] => bar4 [foo5] => bar5 ) I hope this helped someone :) http://php.net/manual/en/function.array-walk-recursive.php

« previous php.notes (#85359) next »