note 71768 deleted from ref.array by nlopess
| From: | nlopess@php.net | Date: | Sun, 17 Dec 2006 19:28:13 +0000 |
| Subject: | note 71768 deleted from ref.array by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-120696@lists.php.net to get a copy of this message | ||
Note Submitter: xixona at voila dot fr
----
I was inspired by zspencer at zacharyspencer dot com into making my own set function, but I wanted
it to return the value if found and false otherwise. I also based myself on his algorithm :
<?
$tab=array();
$tab['a']='lala';
$tab['b']='blabla';
$tab['c']='nono';
echo set($tab, 'b');
// outputs blabla
echo next($tab);
//outputs nono
function set(&$tab, $key){
reset($tab);
while ( ($key != key($tab)) && next($tab) ){}
return current($tab);
}
/*The loop uses the fact that && only evaluates the second term if the first is true.
Therefore, if the first key is the one we seek, the "next" command won't be
evaluated. "next($tab)" also allows the loop to check every element while serving as an
exit condition. (end($tab); next($tab); returns false)*/
?>