note 73449 deleted from control-structures.while by cmb
| From: | cmb@php.net | Date: | Wed, 30 Sep 2020 11:32:26 +0000 |
| Subject: | note 73449 deleted from control-structures.while by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215490@lists.php.net to get a copy of this message | ||
Note Submitter: dominik at deobald dot org
----
@stuart:
There's nothing strange or unexpected about your loop's behaviour.
> So in effect the main while loop is only doing one iteration... and not 4 as expected....
That's the wrong conclusion. The outer "while" does all four iterations. However the
"inner" loop does nothing for the second, third and fourth run.
> I think it would be good to have an explaination of this strange behaviour.
Here it is:
<?PHP
$i=0;
while($i < count($one)) {
while($a = each($two)) {
echo $a[1]." - ".$one[$i].", ";
}
$i++;
}
?>
The "problem" is your use of "each", which reached the last item after the first
iteration of the outer loop. After that, when you come back to the second iteration with the outer
loop, "each" still is at the end of the array $two.
If you add a reset($two) in front of the inner "while", you'll get the result you
expect.