Mega Code Archive

 
Categories / Php / Data Type
 

Displaying Multidimensional Arrays

<?php function display_array($arr, $spacing) {     $widths = array();     foreach ($arr as $row) {         foreach ($row as $key => $col) {             if (!isset($widths[$key]) || (strlen($col) > $widths[$key])) {                 $widths[$key] = strlen($col);             }         }     }     echo '<pre>';     foreach ($arr as $row) {         $count = 0;         foreach ($row as $key => $col) {             if ($count++) {                 echo str_repeat(' ', $spacing);             }             echo str_pad($col, $widths[$key]);         }         echo "\n";     } } $test_array = array(     array('A', 'B', 'C', 'D', 'E'),     array('a', 'b', 'c', 'd', 'e'),     ); display_array($test_array, 3); ?>