Skip to content

Instantly share code, notes, and snippets.

@AucT
Created December 31, 2020 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AucT/05947380d72dfffce21b89f97dc54dc1 to your computer and use it in GitHub Desktop.
Save AucT/05947380d72dfffce21b89f97dc54dc1 to your computer and use it in GitHub Desktop.
Get html table from array of assoc arrays (simple result set from mysql tables)
<?php
function arrayToTable(array $rows, string $tableAttributes = ''): string
{
if (!$rows) {
return '';
}
$rowsHeaders = array_keys($rows[0]);
$table = "<table {$tableAttributes}><tr>";
foreach ($rowsHeaders as $rowsHeader) {
$table .= "<th>{$rowsHeader}</th>";
}
$table .= '</tr>';
foreach ($rows as $row) {
$table .= '<tr>';
foreach ($row as $rowColumn) {
$table .= "<td>{$rowColumn}</td>";
}
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment