* @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 = ' ') { // 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 = "\n"; // The header $table .= "\t"; // Take the keys from the first row as the headings foreach (array_keys($array[0]) as $heading) { $table .= ''; } $table .= "\n"; // The body foreach ($array as $row) { $table .= "\t" ; foreach ($row as $cell) { $table .= ''; } $table .= "\n"; } // End the table $table .= '
' . $heading . '
'; // 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 .= '
'; // Method of output if ($return === false) { echo $table; } else { return $table; } } ?>