note 52741 added to control-structures.while
| From: | chrismushy at osu1 dot php dot net | Date: | Wed, 11 May 2005 13:43:50 +0000 |
| Subject: | note 52741 added to control-structures.while | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89259@lists.php.net to get a copy of this message | ||
Just a note to stuart - the reason for this behaviour is because using the while(value =
each(array)) construct increments the internal counter of the array as its looped through. Therefore
if you intend to repeat the loop, you need to reset the counter. eg:
$one = array("10", "20", "30", "40");
$two = array("a", "b", "c", "d");
$i=0;
while($i < count($one)) {
reset($two);
while($a = each($two)) {
echo $a[1]." - ".$one[$i].", ";
}
$i++;
}
This produces:
a - 10, b - 10, c - 10, d - 10, a - 20, b - 20, c - 20, d - 20, a - 30, b - 30, c - 30, d - 30, a -
40, b - 40, c - 40, d - 40,
----
Manual Page -- http://www.php.net/manual/en/control-structures.while.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52741
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52741&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52741&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52741&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52741&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52741&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52741&report=yes
Search -- http://master.php.net/manage/user-notes.php