Last active
August 24, 2021 14:01
-
-
Save scammi/6759840789988911b3d5472ea2e57daa to your computer and use it in GitHub Desktop.
PHP array to HTML table.
This file contains hidden or 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
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