note 46168 added to control-structures.do.while
| From: | bandman at thecircus dot org | Date: | Fri, 01 Oct 2004 11:00:29 +0000 |
| Subject: | note 46168 added to control-structures.do.while | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-77672@lists.php.net to get a copy of this message | ||
I was having problems iterating through a multidimensional array using a while(next($array)) { }.
The array would always stop short before the while loop got to the end of $array, because I was
doing next() first, which prematurely aged the array index. All I had to do to get it to work was to
reverse the loop into a do { } while(next($array)). Perhaps the code below will make more sense..
<?php
$multiArray = array(array(1,2,3,4),array(1,2,3,4),array(1,2,3,4));
$countIn=0; // Inner Loop
$countOut=0; // Outer Loop
reset($multiArray);
do {
echo "<br>Size of \$multiArray[$countOut] is: " .
count($multiArray[$countOut]);
do {
echo "<br>We are currently at: $countOut-$countIn";
echo "<br>The values are: \$multiArray[$countOut][$countIn] = " .
$multiArray[$countOut][$countIn];
$countIn++;
} while (next($multiArray[$countOut]));
$countIn=0;
$countOut++;
} while (next($multiArray));
?>
I hope this can help someone out there. Thanks!
--Matt Simmons
----
Manual Page -- http://www.php.net/manual/en/control-structures.do.while.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46168
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46168&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46168&report=yes
Search -- http://master.php.net/manage/user-notes.php