Skip to content

Instantly share code, notes, and snippets.

@scammi
Last active August 24, 2021 14:01
Show Gist options
  • Save scammi/6759840789988911b3d5472ea2e57daa to your computer and use it in GitHub Desktop.
Save scammi/6759840789988911b3d5472ea2e57daa to your computer and use it in GitHub Desktop.
PHP array to HTML table.
function array_to_html_table($array)
{
$table = '';
$table .= '<table style="width:100%; border:1px solid black; margin-bottom: 5px" border="1" cellspacing="0">';
foreach($array as $key => $row)
{
if($key == 0) // table header
{
$table .= '<thead><tr>';
foreach($row as $value)
{
$table .= '<th style="text-align: left; border:1px solid black">' . $value . '</th>' ;
}
$table .= '</tr></thead><tbody>';
}
else // table body
{
$table .= '<tr>';
foreach($row as $value)
{
$table .= '<td style="border:1px solid black">' . $value . '</td>';
}
$table .= '</tr>';
}
$table .= '</tbody></table>';
return $table;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment