2017-04-05 90 views
-1

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

警告: Ç非法偏移类型:\ 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; 
} 
+0

提供了错误的指标给了我麻烦太多,当你打印$ this-> jsonObj->参与者[$ value]而不是$ value。顺便给我们倾倒你的东西。 –

+0

当您尝试使用对象或数组作为索引键访问数组索引时,会发生非法偏移类型错误。请给你的数组对象的转储。 –

回答

1

1)$ JSON是你需要首先解码字串。

$json = json_decode($json); 

2)你通过对象需要循环,并得到其成员

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

} 
0

你在这个代码

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