Created
December 31, 2020 14:46
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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