Print JSON output into table - PHP -
i need print json
output in table
{ "response_code":200, "pnr":"6642876935", "train_num":"12792", "train_name":"pnbe sc exp", "doj":" 6- 7-2015", "from_station": { "code":"pnbe" }, "to_station": { "code":"sc" }, "reservation_upto": { "code":"sc" }, "boarding_point": { "code":"pnbe" }, "class":"sl", "no_of_passengers":"1", "chart_prepared":"n", "passengers":[ { "sr":"1", "booking_status":"w\/l 43, gnwl","current_status": "rac 19" } ], "noms":1, "error":null }
thank you,
because json has multiple nested arrays need iterate on aswell. check if our value array if use other foreach loop.
<?php $json = '{"response_code":200,"pnr":"6642876935","train_num":"12792","train_name":"pnbe sc exp ","doj":" 6- 7-2015","from_station":{"code":"pnbe"},"to_station":{"code":"sc"},"reservation_upto":{"code":"sc"},"boarding_point":{"code":"pnbe"},"class":"sl","no_of_passengers":"1","chart_prepared":"n","passengers":[{"sr":"1","booking_status":"w\/l 43,gnwl","current_status":"rac 19"}],"noms":1,"error":null}'; $data = json_decode($json, true); ?> <table> <tr> <td>key</td> <td>value</td> <td>value</td> </tr> <?php foreach($data $key => $value){ if(is_array($value)){ foreach($value $element){ if(is_array($element)){ foreach($element $key2 => $child){?> <tr> <td><?php echo $key; ?></td> <td><?php echo $key2; ?></td> <td><?php echo $child; ?></td> </tr> <?php } } else { ?> <tr> <td><?php echo $key; ?></td> <td><?php echo $element; ?></td> <td></td> </tr> <?php } } } else { ?> <tr> <td><?php echo $key; ?></td> <td><?php echo $value; ?></td> <td></td> </tr> <?php } } ?> </table>
Comments
Post a Comment