Parse JSON to table (Wordpress) Parse JSON to table (Wordpress) wordpress wordpress

Parse JSON to table (Wordpress)


Here is an example on how you can parse this JSON structure into a table

<?php    $data = json_decode('{"appartments":[{"aptnum":"199","design":"open","sqft":"1200","extras":"covered parking","pool":"yes","moveinDate":"2019-01-01 13:12:01","link":"https:\/\/www.demoapts.com\/demo\/199"},{"aptnum":"223","design":"Built Already","sqft":"1800","extras":"covered parking","pool":"yes","moveinDate":"2018-05-09 00:12:01","link":"https:\/\/www.demoapts.com\/demo\/223"}]}');    // Convert JSON string into a PHP object.    $appartments = $data->appartments;    echo('<table>');    if(!empty($appartments)){        echo('<thead><tr>');        // Using the first object to print column names.        foreach($appartments[0] as $key => $value){            echo('<th>' . $key . '</th>');           }        echo('</tr></thead>');        echo('<tbody>');        // Iterate through all appartments and print them as table cells.        foreach($appartments as $appartment){            echo('<tr>');            foreach($appartment as $key => $value){                echo('<td>' . $value . '</td>');               }            echo('</tr>');        }    echo('</tbody></table>');    }?>