3

I need to print JSON output in the 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,

1
  • 1
    Welcome on SO. Please show us what you tried and you best effort to solve your problem yourself. This is somewhat a very simple logic... Commented Jul 2, 2015 at 6:59

2 Answers 2

1

Because your json has multiple nested arrays you need to iterate over those aswell. So we check if our value is an array if so we use an 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 as $key => $value){ 

            if(is_array($value)){
                  foreach($value as $element){  
                        if(is_array($element)){
                              foreach($element as $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>
Sign up to request clarification or add additional context in comments.

Comments

0
<?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>
    </tr>
    <?php foreach($data as $key => $value) : ?>
    <tr>
        <td><?= $key; ?></td>
        <td><?= $value; ?></td>
    <?php endforeach; ?>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.