note 91401 deleted from function.array-walk by danbrown

From: Date: Tue, 09 Jun 2009 20:06:22 +0000
Subject: note 91401 deleted from function.array-walk by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156647@lists.php.net to get a copy of this message
Note Submitter: Matt Kantor ---- @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; } ?>

« previous php.notes (#156647) next »