Skip to content

Instantly share code, notes, and snippets.

@triple-j
Created August 25, 2014 15:56
Show Gist options
  • Save triple-j/233cd6ac1971ab2ab673 to your computer and use it in GitHub Desktop.
Save triple-j/233cd6ac1971ab2ab673 to your computer and use it in GitHub Desktop.
<?php
function array2table( $expression, $return = false ) {
$expression = (array)$expression;
$keys = array_keys($expression[0]);
$html = "<table class=\"arr2tbl\">";
//headers
$html .= "<thead>";
$html .= "<tr>";
foreach ( $keys as $header ) {
$html .= "<th>{$header}</th>";
}
$html .= "</tr>";
$html .= "</thead>";
//content
$html .= "<tbody>";
foreach ( $expression as $row ) {
$html .= "<tr>";
foreach ( $keys as $idx ) {
$html .= "<td>{$row[$idx]}</td>";
}
$html .= "</tr>";
}
$html .= "</tbody>";
$html .= "</table>";
if ( $return ) {
return $html;
} else {
echo $html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment