Skip to content

Instantly share code, notes, and snippets.

@stilliard
Created October 9, 2012 13:24
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save stilliard/3858783 to your computer and use it in GitHub Desktop.
Save stilliard/3858783 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;
}
@stilliard
Copy link
Author

Usage:

echo jsonToTable(json_decode('{"name":"Bob","age":23,"skills":["php","javascript"]}'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment