-
-
Save jeijei4/6ea324e03d994c76b6148117f32c020f to your computer and use it in GitHub Desktop.
JSON to HTML 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 | |
/** | |
* 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