surajkandukuri Posted October 20, 2009 Share Posted October 20, 2009 Hi, I have few arrays namely firstarray(),secondarray(),thirdarray() which are computed based on some php query results. Now I want to display the results of each in a table format for example FIRST COLUMN SECOND COLUMN THIRD COLUMN firstarray(1) secondarray(1) thirdarray(1) firstarray(2) secondarray(2) thirdarray(2) ............. and so on . How to achieve this, The number of rows in the table should be dependent on the number of values in the array. Please tell me how to achieve this... Quote Link to comment https://forums.phpfreaks.com/topic/178402-displaying-values-of-an-array-in-table/ Share on other sites More sharing options...
mikesta707 Posted October 21, 2009 Share Posted October 21, 2009 are the lengths the same? and are they numeric? if so you can do something like echo "<table>" for ($i = 0; $i < count($firstarray); $i++){ echo "<tr>"; echo "<td>".$firstarray[$i]."</td>"; echo "<td>".$secondarray[$i]."</td>"; echo "<td>".$thirdarray[$i]."</td>"; echo "</tr>"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/178402-displaying-values-of-an-array-in-table/#findComment-940802 Share on other sites More sharing options...
xtopolis Posted October 21, 2009 Share Posted October 21, 2009 mikesta707's solution won't show all results if arrays 2 or 3 have more elements than array 1. A "quick" fix would be to change the second line to $max = max(count($firstarray), count($secondarray), count($thirdarray)); for ($i = 0; $i < $max; $i++){ However, you should also add in code to check that the index is not out of range when it goes to print the values in lines 4,5,6... Quote Link to comment https://forums.phpfreaks.com/topic/178402-displaying-values-of-an-array-in-table/#findComment-940846 Share on other sites More sharing options...
mikesta707 Posted October 21, 2009 Share Posted October 21, 2009 thats why i asked if they were the same length. Also, you need to make sure the arrays aren't associative, because if so, the solution will be much more complicated Quote Link to comment https://forums.phpfreaks.com/topic/178402-displaying-values-of-an-array-in-table/#findComment-940847 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.