Displaying Multidimensional Arrays : Multidimensional Array « Data Structure « PHP






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);
?>
  
  








Related examples in the same category

1.Reference element in multidimensional Arrays
2.Defining a Multidimensional Array
3.Looping Through a Multidimensional Array
4.Displaying a Multidimensional Array using For each
5.Accessing multidimensional array elements
6.Creating Multidimensional Arrays
7.Creating a Multidimensional Array
8.Sorting Multidimensional Arrays
9.Multidimensional array element value interpolation
10.Displaying a multidimensional array
11.Find the Maximum Value in a Multidimensional Array
12.Generating fixed-width field data records without
13.Manipulating multidimensional arrays