note 63285 added to function.array-walk-recursive
| From: | egingell at sisna dot com | Date: | Sat, 18 Mar 2006 09:53:31 +0000 |
| Subject: | note 63285 added to function.array-walk-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-106021@lists.php.net to get a copy of this message | ||
Slightly modified version of array_walk_recursive function by "omega13a at sbcglobal dot
net"
$function also modifies the keys in addition to the values.
Usefull if you want to trim() the keys and values of an array.
The $key in $funcname would also have to be passed by referrence.
Named 'array_walk_recursive2' since it is not the same as the PHP5 version, you can rename
it to 'array_walk_recursive' if you are running PHP4 and have no reason to upgrade.
<?
function funcname(&$value, &$key, $userdata = "") {
// Do stuff to $value and $key
}
function array_walk_recursive2(&$input, $funcname, $userdata = "") {
if (!is_callable($funcname)) {
return false;
}
if (!is_array($input)) {
return false;
}
foreach ($input AS $key => $value) {
if (is_array($input[$key])) {
array_walk_recursive2($input[$key], $funcname, $userdata);
} else {
$saved_value = $value;
$saved_key = $key;
if (!empty($userdata)) {
$funcname($value, $key, $userdata);
} else {
$funcname($value, $key);
}
if ($value != $saved_value || $saved_key != $key) {
unset($input[$saved_key]);
$input[$key] = $value;
}
}
}
return true;
}
?>
----
Server IP: 216.194.113.175
Probable Submitter: 65.100.194.45
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.array-walk-recursive.php
Edit -- http://master.php.net/note/edit/63285
Del: integrated -- http://master.php.net/note/delete/63285/integrated
Del: useless -- http://master.php.net/note/delete/63285/useless
Del: bad code -- http://master.php.net/note/delete/63285/bad+code
Del: spam -- http://master.php.net/note/delete/63285/spam
Del: non-english -- http://master.php.net/note/delete/63285/non-english
Del: in docs -- http://master.php.net/note/delete/63285/in+docs
Del: other reasons-- http://master.php.net/note/delete/63285
Reject -- http://master.php.net/note/reject/63285
Search -- http://master.php.net/manage/user-notes.php