note 73449 added to control-structures.while

From: Date: Fri, 23 Feb 2007 16:50:09 +0000
Subject: note 73449 added to control-structures.while
Groups: php.notes 
Request: Send a blank email to php-notes+get-123774@lists.php.net to get a copy of this message
@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. ---- Server IP: 217.160.72.57 Probable Submitter: 88.66.58.12 ---- Manual Page -- http://www.php.net/manual/en/control-structures.while.php Edit -- https://master.php.net/note/edit/73449 Del: integrated -- https://master.php.net/note/delete/73449/integrated Del: useless -- https://master.php.net/note/delete/73449/useless Del: bad code -- https://master.php.net/note/delete/73449/bad+code Del: spam -- https://master.php.net/note/delete/73449/spam Del: non-english -- https://master.php.net/note/delete/73449/non-english Del: in docs -- https://master.php.net/note/delete/73449/in+docs Del: other reasons-- https://master.php.net/note/delete/73449 Reject -- https://master.php.net/note/reject/73449 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#123774) next »