How to loop through json Object using foreach using PHP?

后端 未结 2 503
小蘑菇
小蘑菇 2021-01-29 08:47

How do I use for each to access and print the participant name. The Json object is \"particpants:name\" and it gets populated when uploaded with a tokenized file. I know the tok

相关标签:
2条回答
  • 2021-01-29 09:25

    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)
  • 2021-01-29 09:31

    You are supplying the wrong index in this code

    foreach($this->jsonObj->{'participants'} as $index => $value)
    {
        // $this->html.='<td>' . $this->jsonObj->participants[$value].'</td> ';
    
        // instead
        $this->html.='<td>' . $this->jsonObj->participants[$index].'</td> ';
    
        //or
        //$this->html.='<td>' . $value.'</td> ';
    
    } // foreach
    
    0 讨论(0)
提交回复
热议问题