looping through array

looping through array

am 30.08.2007 17:15:26 von mtuller

I have an array with elements based on 10. In other words, there can
be 10, 20, 30, etc. elements in the array. I want to loop through
elements 0-9, and print them out, and then loop through 10-19, and on
until the end of the array. I know how to loop through an array, but I
am lost as to how to loop through 10 items, and then move to the next
10 until there are no more. Here is the code that I currently have. It
loops though the correct number of times, but I can't get it to go to
the next 10 array items. Here is the code that I have:

$loops = count($b)/10;
for ($i = 0; $i < $loops; $i++)
{
for ($j = 0; $j <10; $j++)
{
echo "???: " . $b[$j] . '
';
}
}




Any tips would be appreciated


Mike

Re: looping through array

am 30.08.2007 18:08:55 von Michael Fesser

..oO(mtuller)

>I have an array with elements based on 10. In other words, there can
>be 10, 20, 30, etc. elements in the array. I want to loop through
>elements 0-9, and print them out, and then loop through 10-19, and on
>until the end of the array. I know how to loop through an array, but I
>am lost as to how to loop through 10 items, and then move to the next
>10 until there are no more.

You could have a look at array_chunk().

>Here is the code that I currently have. It
>loops though the correct number of times, but I can't get it to go to
>the next 10 array items. Here is the code that I have:
>
>$loops = count($b)/10;
>for ($i = 0; $i < $loops; $i++)
>{
> for ($j = 0; $j <10; $j++)
> {
> echo "???: " . $b[$j] . '
';

You have to take into account the outer loop. The key of the current
element in the inner loop would be $i*10+$j, not just $j.

Micha

Re: looping through array

am 30.08.2007 18:56:09 von Shelly

"mtuller" wrote in message
news:1188486926.185999.16360@o80g2000hse.googlegroups.com...
>I have an array with elements based on 10. In other words, there can
> be 10, 20, 30, etc. elements in the array. I want to loop through
> elements 0-9, and print them out, and then loop through 10-19, and on
> until the end of the array. I know how to loop through an array, but I
> am lost as to how to loop through 10 items, and then move to the next
> 10 until there are no more. Here is the code that I currently have. It
> loops though the correct number of times, but I can't get it to go to
> the next 10 array items. Here is the code that I have:
>
> $loops = count($b)/10;
> for ($i = 0; $i < $loops; $i++)
> {
> for ($j = 0; $j <10; $j++)
> {
> echo "???: " . $b[$j] . '
';
> }
> }
>
>
>
>
> Any tips would be appreciated
>
>
> Mike

for ($i = 0, j=1; $i < count($b); $i++, $j++) {
if ($j == 10) {
echo "???: " . $b[$i] . '
';
$j = 0;
}
....other stuff ....
}

--
Shelly