Jump to content

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

:)
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.