Skip to content

Instantly share code, notes, and snippets.

@jeijei4
Forked from stilliard/jsonToTable.php
Created October 15, 2019 18:00
Show Gist options
  • Save jeijei4/6ea324e03d994c76b6148117f32c020f to your computer and use it in GitHub Desktop.
Save jeijei4/6ea324e03d994c76b6148117f32c020f to your computer and use it in GitHub Desktop.
JSON to HTML table
<?php
/**
* JSON data to html table
*
* @param object $data
*
*/
function jsonToTable ($data)
{
$table = '
<table class="json-table" width="100%">
';
foreach ($data as $key => $value) {
$table .= '
<tr valign="top">
';
if ( ! is_numeric($key)) {
$table .= '
<td>
<strong>'. $key .':</strong>
</td>
<td>
';
} else {
$table .= '
<td colspan="2">
';
}
if (is_object($value) || is_array($value)) {
$table .= jsonToTable($value);
} else {
$table .= $value;
}
$table .= '
</td>
</tr>
';
}
$table .= '
</table>
';
return $table;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment