How do I loop through JSON array

1) $json is a string you need to decode it first.

$json = json_decode($json);

2) you need to loop through the object and get its members

foreach($json as $obj){
   echo $obj->name;
   .....

}

another example:

  <?php

  //lets make up some data:
  $udata['user'] = "mitch";
  $udata['date'] = "2006-10-19";
  $udata['accnt'] = "EDGERS";
  $udata['data'] = $udata; //array inside
  var_dump($udata); //show what we made

  //lets put that in a file
  $json = file_get_contents('file.json');
  $data = json_decode($json);
  $data[] = $udata;
  file_put_contents('file.json', json_encode($data));

  //lets get our json data
  $json = file_get_contents('file.json');
  $data = json_decode($json);
  foreach ($data as $obj) {
        var_dump($obj->user);
  }

Leave a Comment