Page 1 of 1

printing a 2D array in a multi column table

Posted: Tue Mar 04, 2008 6:22 pm
by gammaman
How can I change the following code

Code: Select all

 
<?php
function print_array ($array_name)
{
  
  echo ("<table border =\"1\">");
  foreach ($array_name as $itm=>$var)
  {
   
  foreach ($var as $new){
  
    echo "<tr><td>$new</td></tr>";
  }
  
  
  }   
  echo "</table>"; 
}   
$my_array=array(array("NY", "TX"),
             array("WI","MD"));    
   print_array($my_array);      
 
?>
 
To look like this

Re: printing a 2D array in a multi column table

Posted: Tue Mar 04, 2008 7:45 pm
by nincha
replace your foreach loop with a for loop so you have control over the index of your array. For them html part just add another td tag, ie:<tr><td></td><td></td></tr>