-
Détails
-
Catégorie : PHP
function array2table($array, $recursive = true, $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 = '<p>';
// The header<br>
$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";
}
$table .= '</p><br><table><br><tbody><br><tr><th>' . $heading . '</th></tr><br><tr><br><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></tr></tbody></table>';
return $table;
}
print array2table($a);
?>