note 114574 added to function.array-walk-recursive
| From: | gk at anuary dot com | Date: | Fri, 07 Mar 2014 21:29:32 +0000 |
| Subject: | note 114574 added to function.array-walk-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198826@lists.php.net to get a copy of this message | ||
array_walk_recursive itself cannot unset values. Even though you can pass array by reference,
unsetting the value in the callback will only unset the variable in that scope.
<?php
/**
* http://uk1.php.net/array_walk_recursive
implementation that is used to remove nodes from the array.
*
* @param array The input array.
* @param callable $callback Function must return boolean value indicating whether to remove the
node.
* @return array
*/
function walk_recursive_remove (array $array, callable $callback) {
foreach ($array as $k => $v) {
if (is_array($v)) {
$v = walk_recursive_remove($v, $callback);
} else {
if ($callback($v, $k)) {
unset($array[$k]);
}
}
}
return $array;
}
?>
An up to date implementation of the above function can be looked up from https://github.com/gajus/marray/blob/master/src/marray.php#L116.
----
Server IP: 87.117.229.81
Probable Submitter: 46.28.49.140
----
Manual Page -- http://php.net/manual/en/function.array-walk-recursive.php
Edit -- https://master.php.net/note/edit/114574
Del: integrated -- https://master.php.net/note/delete/114574/integrated
Del: useless -- https://master.php.net/note/delete/114574/useless
Del: bad code -- https://master.php.net/note/delete/114574/bad+code
Del: spam -- https://master.php.net/note/delete/114574/spam
Del: non-english -- https://master.php.net/note/delete/114574/non-english
Del: in docs -- https://master.php.net/note/delete/114574/in+docs
Del: other reasons-- https://master.php.net/note/delete/114574
Reject -- https://master.php.net/note/reject/114574
Search -- https://master.php.net/manage/user-notes.php