note 91401 added to function.array-walk
| From: | MattKantor at osu1 dot php dot net | Date: | Tue, 09 Jun 2009 20:03:11 +0000 |
| Subject: | note 91401 added to function.array-walk | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156646@lists.php.net to get a copy of this message | ||
@bisqwit (http://php.net/manual/en/function.array-walk.php#45380):
Here's an alternative that can alter array keys as well as values:
(WARNING: this code has not been rigorously tested.)
<?php
// Function: array_walk_keys
// Works just like array_walk (http://php.net/manual/en/function.array-walk.php)
// but allows passing the key by reference and modifying it.
// The iteration order of the original array will be preserved even if you
// modify keys.
function array_walk_keys(&$array, $funcname, &$userdata = null) {
$original = $array;
$array = array();
$success = true;
if(!function_exists($funcname)) {
// For some reason array_walk returns true if the callback doesn't exist.
// I'm replicating this behavior even though I don't agree with it.
trigger_error('Unable to call '.$funcname.'() - function does not exist',
E_USER_WARNING);
} else {
foreach($original as $key => $value) {
$funcname(&$value, &$key, &$userdata);
if(isset($array[$key])) {
trigger_error(
'There was a collision for the key "'.$key.'"; the second instance
was not set',
E_USER_WARNING
);
$success = false;
} else $array[$key] = $value;
}
}
return $success;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 208.105.225.42
----
Manual Page -- http://www.php.net/manual/en/function.array-walk.php
Edit -- https://master.php.net/note/edit/91401
Del: integrated -- https://master.php.net/note/delete/91401/integrated
Del: useless -- https://master.php.net/note/delete/91401/useless
Del: bad code -- https://master.php.net/note/delete/91401/bad+code
Del: spam -- https://master.php.net/note/delete/91401/spam
Del: non-english -- https://master.php.net/note/delete/91401/non-english
Del: in docs -- https://master.php.net/note/delete/91401/in+docs
Del: other reasons-- https://master.php.net/note/delete/91401
Reject -- https://master.php.net/note/reject/91401
Search -- https://master.php.net/manage/user-notes.php