note 52741 deleted from control-structures.while by cmb
| From: | cmb@php.net | Date: | Wed, 30 Sep 2020 11:32:17 +0000 |
| Subject: | note 52741 deleted from control-structures.while by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215488@lists.php.net to get a copy of this message | ||
Note Submitter: chris mushy
----
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,