Re: Looping through array
| From: | Richard Lynch | Date: | Mon, 20 Nov 2006 23:42:03 +0000 |
| Subject: | Re: Looping through array | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-244915@lists.php.net to get a copy of this message | ||
On Thu, November 16, 2006 3:14 pm, Ashley M. Kirchner wrote:
> Darrell Brogdon wrote:
>> So in other words, you have an array like $arr =
>> array(1,2,3,4,5,6,7,8,9,10) but you want it to render on the page
>> as:
>>
>> 5 4 3 2 1
>> 10 9 8 7 6
>>
>> right?
> That would be correct. James Tu provided a solution that I think
> will work. I'm always open to other suggestions of course.
Any chance of just getting the stuff in the right order in the first
place?... :-)
If not, here is what I would do:
<?php
$array = array(1,2,3,4,5,6,7,8,9,10);
$array = array_reverse($array);
$row_one = array_slice($array, 0, 5));
$row_two = array_slice($array, 5));
?>
<table>
<tr><td><?php echo implode("</td><td>",
$row_one);?></td></tr>
<tr><td><?php echo implode("</td><td>",
$row_two);?></td></tr>
</table>
This would be fairly trivial to extend to multiple rows:
<?php
//assume input $array in 'order' as above,
//only with unknown length
$array = array_reverse($array);
?>
<table>
</php
for ($i = 0; $i < count($array); $i += 5){
echo " <tr><td>", implode("</td><td>",
array_slice($array, $i,
5)), "</td></tr>\n";
}
?>
</table>
YMMV
--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?