number9dream Posted July 7, 2006 Share Posted July 7, 2006 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.elemon yellowApple greenbanana yellowDoes anyone have any advice on how I might achieve this?Many thanks :) Quote Link to comment https://forums.phpfreaks.com/topic/13972-displaying-data-from-two-arrays-simultaneously/ Share on other sites More sharing options...
kenrbnsn Posted July 7, 2006 Share Posted July 7, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/13972-displaying-data-from-two-arrays-simultaneously/#findComment-54507 Share on other sites More sharing options...
number9dream Posted July 7, 2006 Author Share Posted July 7, 2006 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!:) Quote Link to comment https://forums.phpfreaks.com/topic/13972-displaying-data-from-two-arrays-simultaneously/#findComment-54521 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.