Skip to content

Instantly share code, notes, and snippets.

@b1ek
Created October 24, 2022 08:21
Show Gist options
  • Save b1ek/b760fa413a7859bb42e30f47c2ec3153 to your computer and use it in GitHub Desktop.
Save b1ek/b760fa413a7859bb42e30f47c2ec3153 to your computer and use it in GitHub Desktop.
PHP array to table
<?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