In Action

Php Toycode - Turning a two-dimensional array into an html table

You can think of a two-dimensional array as a spreadsheet worksheet. It is nothing more than an array that consists of rows and columns. In fact, this example takes a 2D array and turns it into an html table. This example purposely hardcodes the 2D array as it is easier to see what is going on. I have provided to additional variations of this example.

Php in Action



Php Code

Just cut-n-paste to reuse.

//this is a classic 2-dimensional anonymous array
$table = array( array("A1", "B1" , "C1"),
                array("A2", "B2" , "B2"),
                array("A3", "B3" , "C3") 
echo "<pre>" .            
'$table = array( array("a1", "b1" , "c1"),
                array("a2", "b2" , "c2"),
                array("a3", "b3" , "c3") 
             );' . "</pre><br />" ;
echo "get single element <br />";            
echo '$table[2][1] = ' . $table[2][1] . "<br /><br />";

echo "build html table from a 2d array <br />";
foreach ($table as $rows => $row)
	echo "<table border='1'><tr>";
	foreach ($row as $col => $cell)
		echo "<td>" . $cell . "</td>";
  echo "</tr></table>";