Re: Looping through array

From: Date: Thu, 16 Nov 2006 20:25:33 +0000
Subject: Re: Looping through array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-244742@lists.php.net to get a copy of this message
Ashley M. Kirchner wrote:
Say I have an array containing ten items, and I want to display them in a table as follows:
    5 4 3 2 1
10 9 8 7 6 What's the best way to loop through that array to do that? My thinking gets me to create a loop for 5 through 1, repeated twice, and the second time I add '5' to the index value. There's got to be a saner way... Something like this perhaps...
$arr = array(...); $per_row = 5; $elem = count($arr); for($i=0; $i<$elem; $i++) { if( $i == 0 )
      echo "<tr>";
if( $i % $per_row == 0 )
      echo "</tr><tr>";
echo "<td>$arr[$i]</td>"; }

« previous php.general (#244742) next »