note 19072 added to function.each
| From: | jordan_howe at hotmail dot com | Date: | Thu, 14 Feb 2002 20:48:43 +0000 |
| Subject: | note 19072 added to function.each | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-26622@lists.php.net to get a copy of this message | ||
You must even use reset() even if you use each() in a separate function.
For example:
function do_stuff($element) {
global $arr2;
reset ($arr2);
while (list($name,$number) = each($arr2)) {
if ($number == $element) {
echo ("$name is an index in arr2 for this element!\n");
}
}
}
$arr = array(1,5,9,13);
$arr2 =
array('first'=>1,'second'=>3,'third'=>5,'fourth'=>7);
foreach ($arr as $element) {
/* this will fail if do_stuff() doesn't call reset() */
do_stuff($element);
}
This is a nasty bug. You would think that each time you called a function, it would start fresh as
if it had never been called before. I think the pointer should automatically be reset upon exiting
a function.
--
http://www.php.net/manual/en/function.each.php
http://master.php.net/manage/user-notes.php?action=edit+19072
http://master.php.net/manage/user-notes.php?action=delete+19072
http://master.php.net/manage/user-notes.php?action=reject+19072