note 46168 deleted from control-structures.do.while by tomsommer
| From: | tomsommer@php.net | Date: | Fri, 01 Oct 2004 20:47:12 +0000 |
| Subject: | note 46168 deleted from control-structures.do.while by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77716@lists.php.net to get a copy of this message | ||
Note Submitter: bandman at thecircus dot org
----
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