-
-
Save earth3300/954da0535601232b55f205e843265b26 to your computer and use it in GitHub Desktop.
JSON to HTML table
This file contains hidden or 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; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useage:
echo jsonToTable(json_decode('{"name":"Bob","age":23,"skills":["php","javascript"]}'));