note 48181 added to function.array-walk-recursive
| From: | (at) | Date: | Tue, 14 Dec 2004 19:08:54 +0000 |
| Subject: | note 48181 added to function.array-walk-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-81792@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:
//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 :)
----
Manual Page -- http://www.php.net/manual/en/function.array-walk-recursive.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+48181
Delete -- http://master.php.net/manage/user-notes.php?action=delete+48181&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48181&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48181&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48181&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48181&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48181&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+48181&report=yes
Search -- http://master.php.net/manage/user-notes.php