How do I loop through JSON array

后端 未结 2 1100
鱼传尺愫
鱼传尺愫 2020-12-02 21:01

How do I traverse and display the names in the following JSON using CodeIgniter?



        
相关标签:
2条回答
  • 2020-12-02 21:17

    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);
      }
    
    0 讨论(0)
  • 2020-12-02 21:22

    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;
       .....
    
    }
    
    0 讨论(0)
提交回复
热议问题