Jump to content

Displaying Values of an Array in Table


surajkandukuri

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

Archived

This topic is now archived and is closed to further replies.

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