note 71768 added to ref.array
| From: | xixonaatvoiladotfr at osu1 dot php dot net | Date: | Thu, 14 Dec 2006 22:41:10 +0000 |
| Subject: | note 71768 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120618@lists.php.net to get a copy of this message | ||
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)*/
?>
----
Server IP: 195.221.21.36
Probable Submitter: 83.156.66.20
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/71768
Del: integrated -- https://master.php.net/note/delete/71768/integrated
Del: useless -- https://master.php.net/note/delete/71768/useless
Del: bad code -- https://master.php.net/note/delete/71768/bad+code
Del: spam -- https://master.php.net/note/delete/71768/spam
Del: non-english -- https://master.php.net/note/delete/71768/non-english
Del: in docs -- https://master.php.net/note/delete/71768/in+docs
Del: other reasons-- https://master.php.net/note/delete/71768
Reject -- https://master.php.net/note/reject/71768
Search -- https://master.php.net/manage/user-notes.php