Aidan's PHP Repository

A repository for PHP functions and classes ...

function.array2table.php

Translate a result array into a HTML table

  • Author: Aidan Lister <aidan@php.net>
  • Version: 1.3.1
  • Link: http://aidanlister.com/repos/v/function.array2table.php
  • Views: 19151
  • Downloads: 2256

Source

Download this script <?php
/**
 * Translate a result array into a HTML table
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.3.1
 * @link        http://aidanlister.com/repos/v/function.array2table.php
 * @param       array  $array      The result (numericaly keyed, associative inner) array.
 * @param       bool   $recursive  Recursively generate tables for multi-dimensional arrays
 * @param       bool   $return     return or echo the data
 * @param       string $null       String to output for blank cells
 */
function array2table($array, $recursive = false, $return = false, $null = '&nbsp;')
{
    // Sanity check
    if (empty($array) || !is_array($array)) {
        return false;
    }
 
    if (!isset($array[0]) || !is_array($array[0])) {
        $array = array($array);
    }
 
    // Start the table
    $table = "<table>\n";
 
    // The header
    $table .= "\t<tr>";
    // Take the keys from the first row as the headings
    foreach (array_keys($array[0]) as $heading) {
        $table .= '<th>' . $heading . '</th>';
    }
    $table .= "</tr>\n";
 
    // The body
    foreach ($array as $row) {
        $table .= "\t<tr>" ;
        foreach ($row as $cell) {
            $table .= '<td>';
 
            // Cast objects
            if (is_object($cell)) { $cell = (array) $cell; }
            
            if ($recursive === true && is_array($cell) && !empty($cell)) {
                // Recursive mode
                $table .= "\n" . array2table($cell, true, true) . "\n";
            } else {
                $table .= (strlen($cell) > 0) ?
                    htmlspecialchars((string) $cell) :
                    $null;
            }
 
            $table .= '</td>';
        }
 
        $table .= "</tr>\n";
    }
 
    // End the table
    $table .= '</table>';
 
    // Method of output
    if ($return === false) {
        echo $table;
    } else {
        return $table;
    }
}
 
?>

Example

<pre>
<?php
require_once 'function.array2table.php';
 
// Some random data
$data[0]['Foo'] = 'Data 1';
$data[0]['Bar'] = 'Data 2';
$data[0]['Baz'] = 'Data 3';
 
$data[1]['Foo'] = 'Data 4';
$data[1]['Bar'] = 'Data 5';
$data[1]['Baz'] = 'Data 6';
 
$data[2]['Foo'] = 'Data 7';
$data[2]['Bar'] = 'Data 8';
$data[2]['Baz'] = $data[0];
 
// Draw it
array2table($data, true);
?>
</pre>

Output

FooBarBaz
Data 1Data 2Data 3
Data 4Data 5Data 6
Data 7Data 8
FooBarBaz
Data 1Data 2Data 3

Comments

June 21st, 2005
Hello and thanks for the great work. I made a patch that shows the datatype of the array-element as a tooltip. Just replace the else-part after "if($recursive==true..." with: '.( (strlen($cell) > 0) ? htmlspecialchars((string) $cell) : $null) .''; ?> regards, adrian