如何使用PHP使用foreach遍历json对象? [英] How to loop through json Object using foreach using PHP?

查看:222
本文介绍了如何使用PHP使用foreach遍历json对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何分别使用它们来访问和打印参与者的姓名. Json对象是"particpants:name",并且在上载带有标记化文件时会被填充.我知道标记化的文件确实创建了成功的JSON obj.它是在foreach循环中给我带来麻烦的.

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 tokenized file does create a successful JSON obj. It is inside the foreach loop that gives me trouble.

警告:输入错误的偏移量 第100行上的C:\ xampp \ htdocs \ nkmoorth \ EthicsRound1.php

Warning: Illegal offset type in C:\xampp\htdocs\nkmoorth\EthicsRound1.php on line 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是您需要首先对其进行解码的字符串.

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

$json = json_decode($json);

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

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

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

}

这篇关于如何使用PHP使用foreach遍历json对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆