Jump to content


Photo

Displaying data from two arrays simultaneously


  • Please log in to reply
2 replies to this topic

#1 number9dream

number9dream
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 July 2006 - 06:56 PM

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 :)

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 July 2006 - 07:06 PM

If both arrays have the same number of elements you can do something like this:
<?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>';
?>

Ken

#3 number9dream

number9dream
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 July 2006 - 07:43 PM

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!

:)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users