So I’m really new to working with json files and coding in general. I’ve tried to wrap my head around this problem for hours now and I can’t find any solution to it.
I have this .json file and want to make a HTML table from it:
{ "template_commands":{ "web":[ "Webadded cmds are working!", "Rights['0']" ], "streamer":[ "This is only for Streamers!", "Rights['3']" ], "admin":[ "This is only for Admins!", "Rights['2']" ], "mod":[ "This is only for mods", "Rights['1']" ], "taka":[ "I love you", "Rights['2']" ] }, "doggo_counter":0, "admins":{ "tauru":"name", "jutlufa":"name" } }
It should look a little bit like this:
cmd_name | cmd | rights |
---|---|---|
web | Webadded cmds are working! | 1 |
I don’t need the “doggo_counter” or “admins” part of the file. Just the “template_commands. Is this even possible with that kind of file?
Answer
<?php $objs = json_decode(file_get_contents('data.json'),true); $commands = $objs['template_commands']; echo "<table border='1'>"; //table bordered echo "<tr><th>cmd_name</th><th>cmd</th><th>Rights</th></tr>"; //the header of table foreach($commands as $cmd_name => $cmds) { echo "<tr><td>$cmd_name</td> <td>$cmds[0]</td> <td>".preg_replace('/[^0-9]+/','', $cmds[1])."</td></tr>";//only digits } echo "</table>"; ?>