Zend - The PHP Company




Arrays

Add Code


a more readable option that "print_r"  

Type: code fragment
Added by: Saibot
Entered: 01/02/2001
Last modified: 02/11/2008
Rating: **** (9 votes)
Views: 9620
This is a really simple function that allows one to display array data in a more readable way than with the standard print_r function


<?
  
/*
    Function coded by Stephan Pirson
    contact: saibot@hesperia-mud.org
  */
  
  
function print_a$TheArray )
  { 
// Note: the function is recursive
    
echo "<table border=1>n";
    
    
$Keys array_keys$TheArray );
    foreach( 
$Keys as $OneKey )
    {
      echo 
"<tr>n";
      
      echo 
"<td bgcolor='#727450'>";
      echo 
"<B>" $OneKey "</B>";
      echo 
"</td>n";
      
      echo 
"<td bgcolor='#C4C2A6'>";
        if ( 
is_array($TheArray[$OneKey]) )
          
print_a($TheArray[$OneKey]);
        else
          echo 
$TheArray[$OneKey];
      echo 
"</td>n";
      
      echo 
"</tr>n";
    }
    echo 
"</table>n";
  }
?>


Usage Example


Give it this to eat and compare with print_r:

  $testa = Array( "blah" => 1, "Blah2" => Array ( 1, 2, 3, "Hmmm" => "Sure", Array(2, 1, Array( "count" => 2), 1 ), 8 ), "Blah3" => 3, "An Array" => Array("Test"=>1, "Test2" => 2, "Test3" => Array( "blah", 3, "someone1" => "me", 7 ) ) );
  print_a($testa);


Rate This Script





Search



This Category All Categories