printing a 2D array in a multi column table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

printing a 2D array in a multi column table

Post 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
Attachments
2d.png
2d.png (4.7 KiB) Viewed 324 times
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

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

Post 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>
Post Reply