Jump to content

Archived

This topic is now archived and is closed to further replies.

number9dream

Displaying data from two arrays simultaneously

Recommended Posts

Hi  :)

I have two arrays which I want to display in adjacent table columns. Unfortunately I can't find a way to loop through both of them at once. I've tried to use two list statements together and also nesting two while loops, but neither works.

Let's say my first array contains "lemon", "apple" and "banana". My second array contains "yellow", "green" and "yellow". I want to loop through them both a display them in a table, i.e

lemon    yellow
Apple    green
banana  yellow

Does anyone have any advice on how I might achieve this?

Many thanks :)

Share this post


Link to post
Share on other sites
If both arrays have the same number of elements you can do something like this:
[code]<?php
$arr1 = array("lemon", "apple", "banana");
$arr2 = array("yellow", "green", "yellow");
echo '<div style="display:block;width:80%">';
for($i=0;$i<count($arr1);$i++) {
    echo '<span style="width:48%;float:left;display:block">' . $arr1[$i] . '</span>';
    echo '<span style="width:48%;float:left;display:block;border-left: 1px solid black;">' . $arr2[$i] . '</span>';
}
echo '</div>';
?>[/code]

Ken

Share this post


Link to post
Share on other sites
That's fantastic!  :)

You've no idea how long I've being trying to get this to work ;)

I altered the code slightly, as the output is tabular data, so a table was more appropriate than DIVs, but other than that it was exactly what I needed.

Thanks again!

:)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.