snake_l 发表于 2017-4-2 13:37:08

PHP中遍历stdclass object 及 json

  (从网上找的模拟实例)需要操作的数据:

$test=Array
(
=> stdClass Object
(
=> 最快的车,Bloodhound,SSC
=> 48326888
)
)


从网上查到的方法是 用get_object_vars 把类类型转换成数组 然后在用foreach 遍历即可  $array = get_object_vars($test);
  $json= '[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","name":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]';
  首先要用 json_decode 对 JSON 格式的字符串进行编码,
  $students = json_decode($json);
  直接在PHP文件用$students :
  for($i=0;$i<count($students);$i++){

echo "姓名:".$students[$i]['name']."年龄:".$students[$i]['age']."专业:".$students[$i]['subject']."<br/>";

}
  则报错如下:
  Fatal error: Cannot use objectof type stdClass as array in
D:\wamp\www\test.phpon line 18
  这时候打印一下 $students :
  var_dump($students);
  会输出:
  array(2) {
  =>
  object(stdClass)#2 (4) {
  ["id"]=> string(1)"1"
  ["name"]=> string(9)"张雪梅"
  ["age"]=> string(2)"27"
  object(stdClass)#3 (4) {
  ["subject"]=>string(24) "计算机科学与技术"
  }
  =>
  ["id"]=> string(1)"2"
  ["name"]=> string(9)"张沛霖"
  ["age"]=> string(2)"21"
  ["subject"]=> string(12) "软件工程"
  }
  }
  可见,返回的结果是 object 而非 array。应以对象形式访问:
  foreach($students as $obj){

echo "姓名:".$obj->name."年龄:".$obj->age."专业:".$obj->subject."<br/>";

}
  输出结果为:
  姓名:张雪梅 年龄:27 专业:计算机科学与技术

姓名:张沛霖 年龄:21 专业:软件工程


  mixedjson_decode ( string$json [, bool$assoc ] )
  说明:接受一个 JSON 格式的字符串并且把它转换为 PHP 变量。
  json_decode 可接收两个参数:
  json:待解码的jsonstring 格式的字符串。
  assoc:当该参数为 TRUE 时,将返回 array 而非 object 。
  $students = json_decode($json,true);
  这时打印一下 $students :
  var_dump($students);
  输出:
  array(2) {
  =>
  array(4) {
  ["id"]=> string(1)"1"
  ["name"]=> string(9)"张雪梅"
  ["age"]=> string(2)"27"
  ["subject"]=>string(24) "计算机科学与技术"
  }
  =>
  array(4) {
  ["id"]=> string(1)"2"
  ["name"]=> string(9)"张沛霖"
  ["age"]=> string(2)"21"
  ["subject"]=>string(12) "软件工程"
  }
  }
  这时,$students 就是个数组了,可以直接用:
  for($i=0;$i<count($students);$i++){

echo "姓名:".$students[$i]['name']."年龄:".$students[$i]['age']."专业:".$students[$i]['subject']."<br/>";

}
  输出结果为:
  姓名:张雪梅 年龄:27 专业:计算机科学与技术

姓名:张沛霖 年龄:21 专业:软件工程
  总结:
  在PHP代码中处理JSON 格式的字符串的两种方法:
  方法一:
  $json= '[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","name":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]';
  $students= json_decode($json);//得到的是 object
  foreach($studentsas $obj){
  echo "姓名:".$obj->name."&nbsp;&nbsp;&nbsp;年 龄:".$obj->age."&nbsp;&nbsp;&nbsp;专 业:".$obj->subject."<br />";

}
  方法二:
  $json= '[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","name":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]';
  $students= json_decode($json, true);//得到的是 array
  for($i=0;$i<count($students);$i++){

echo "姓名:".$students[$i]['name']."&nbsp;&nbsp;&nbsp;年 龄:".$students[$i]['age']."&nbsp;&nbsp;&nbsp;专 业:".$students[$i]['subject']."<br />";

}
页: [1]
查看完整版本: PHP中遍历stdclass object 及 json