如何使用 PHP 使用 foreach 循环 json 对象?

2024-01-09

我如何使用每个来访问和打印参与者姓名。 Json 对象是“particpants:name”,当使用标记化文件上传时,它会被填充。我知道标记化文件确实创建了一个成功的 JSON 对象。正是在 foreach 循环内部给我带来了麻烦。

警告:输入非法偏移量 C:\xampp\htdocs\nkmoorth\EthicsRound1.php 第 100 行

 protected function setMiddleHTML()
{
  //temp

  $this->html.='<div id="middleContainer">';
  $this->jsonObj = json_decode($_POST['fileContents']);
    $count=sizeOf($this->jsonObj->{'participants'}) -1;
  for($i =0; $i<$count; $i++) //should be numSections
   {
    $this->html .= '<tables class="section">';
      foreach($this->jsonObj->{'participants'} as $index => $value)
      {
      $this->html.='<td>' . $this->jsonObj->participants[$value].'</td> ';
      } // foreach
    $this->html .= '</table>';



  }// for    } // setMiddleHTML()

          $this->html.='</div>'; // closing middle Container;
 }

1) $json 是一个字符串,您需要先对其进行解码。

$json = json_decode($json);

2)您需要循环遍历对象并获取其成员

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

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 PHP 使用 foreach 循环 json 对象? 的相关文章