Created
October 24, 2022 08:21
-
-
Save b1ek/b760fa413a7859bb42e30f47c2ec3153 to your computer and use it in GitHub Desktop.
PHP array to table
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 arr2table($array, $headers = true) { | |
$buffer = '<table><tbody><tr>' . ($headers ? '<td>Index</td>' : ''); | |
foreach($array as $i => $val) { | |
$buffer .= "<td>$i</td>"; | |
} | |
$buffer .= '</tr>' . ($headers ? '<td>Value</td>' : ''); | |
foreach($array as $i => $val) { | |
$buffer .= '<td>' . print_r($val, true) . '</td>'; | |
} | |
$buffer .= '</tr></tbody></table>'; | |
return $buffer; | |
} | |
echo arr2table(array('id' => 1, 'message' => 'hi')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment