• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Print Contents of Mutlidimensional Array as HTML Table

I have a multidimensional array that is returned from parsing a file. Here's the output from print_r of the first two sub-arrays.

Array ( [0] => Array ( [FIELD1] => SYSTEM [FIELD2] => $$ [FIELD3] => 127.0.0.1 [FIELD4] => 33014 [FIELD5] => 38837 [FIELD6] => 38858.8 [FIELD7] => [FIELD8] => [FIELD9] => 100 [FIELD10] => 0 [FIELD11] => 0 [FIELD12] => ) [1] => Array ( [FIELD1] => A18 [FIELD2] => eA [FIELD3] => 193.111.174 [FIELD4] => 58453 [FIELD5] => 38858.517977338 [FIELD6] => 38858 [FIELD7] => 2..22787 [FIELD8] => 12121 [FIELD9] => 100 [FIELD10] => 2 [FIELD11] => 1 [FIELD12] => )

What I need is to put this into an HTML table like

Field 1 Field 2 Field 3 Field 4
Field 1 Field 2 Field 3 Field 4

What code can be used to do this? All this stuff is doing my head in...

0
jbreg
Asked:
jbreg
  • 2
  • 2
1 Solution
 
dr_dedoCommented:
echo "<table>";
foreach ($c as $f){
                echo "<tr>";
      echo "<td>$f[0]</td><td>$f[1]</td><td>$f[2]</td><td>$f[3]</td>";
                echo "</tr>";
}
echo "</table>";
0
 
jbregAuthor Commented:
Sorry, that returns "Invalid argument for foreach".

In addition, there are 12 columns, and the array is multidimensional as you can see so in fact you need two foreach statements no?
0
 
dr_dedoCommented:
Invalid argument for foreach cause you have to assign and array first. Also, i thought u want the first 4 fields only
<?
$c = array( 0=>array(11,12,13,14,15,16,17,18,19,110),
                 1=>array(21,22,23,24,25,26,27,28,29,210)
                );
echo "<table>";
foreach ($c as $f){
                echo "<tr>";
                foreach ($f as $a){
                      echo "<td>$a</td>";
                }
                echo "</tr>";
}
echo "</table>";
?>
0
 
jbregAuthor Commented:
Works great, thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now