Jump to content

Recommended Posts

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...

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/178402-displaying-values-of-an-array-in-table/
Share on other sites

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>";

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...

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.