php stdClass Object 问题
Array ( [0] => stdClass Object ( [term_id] => 3 [name] => apache [slug] => apache [term_group] => 0 [term_taxonomy_id] => 3 [taxonomy] => category [description] => [parent] => 0 [count] => 1 [cat_ID] => 3 [category_count] => 1 [category_description] => [cat_name] => apache [category_nicename] => apache [category_parent] => 0 ) [1] => stdClass Object ( [term_id] => 1 [name] => PHP [slug] => php [term_group] => 0 [term_taxonomy_id] => 1 [taxonomy] => category [description] => [parent] => 0 [count] => 1 [cat_ID] => 1 [category_count] => 1 [category_description] => [cat_name] => PHP [category_nicename] => php [category_parent] => 0 ) )
二维数组stdClass Object是一组json对象,如果要调用里面的数据,就要这样调用,
foreach($categories as $key => $value){
$cat = $value->term_id;
$name = $value->name;
} 或者json_decode第二个参数设为true:
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) {
[0]=>
array(4) {
["id"]=> string(1)"1"
["name"]=> string(9)"张雪梅"
["age"]=> string(2)"27"
["subject"]=>string(24) "计算机科学与技术"
}
[1]=>
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." 年 龄:".$obj->age." 专 业:".$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']." 年 龄:".$students[$i]['age']." 专 业:".$students[$i]['subject']."<br />";
}
参考:http://blog.csdn.net/wuhanzaizai/article/details/6874845
php stdClass Object 问题的更多相关文章
- php如何遍历多维的stdClass Object 对象,php的转换成数组的函数只能转换外面一丛数组
php如何遍历多维的stdClass Object 对象,php的转换成数组的函数只能转换外面一丛数组 (2012-09-10 19:58:49) 标签: 杂谈 分类: 网页基础知识 php如何遍历多 ...
- PHP中遍历stdclass object 及 json 总结[中国航天神舟十号以json形式向地面返回数据]
$test=Array ( [0] => stdClass Object ( [tags] => 最快的车,Bloodhound,SSC [id] => 48326888 11 从网 ...
- PHP中遍历stdclass object 及 json
原文:PHP中遍历stdclass object 及 json (从网上找的模拟实例)需要操作的数据: $test=Array ( [0] => stdClass Object ( [tags] ...
- 数组中的stdClass Object如何访问
使用print_r($data)输出结果为 Array ( [0] => stdClass Object ( [color_item_no] => 1 [color_name] => ...
- stdClass object 数据获取方法
$data = stdClass Object ( [code] => [data] => stdClass Object ( [country] => 未分配或者内网IP [cou ...
- php json 解析有stdClass Object 解决办法
php json 解析有stdClass Object mixed json_decode ( string $json [, bool $assoc = false [, int $depth = ...
- PHP------数组和对象相互转化,stdClass Object转array
数组转JSON PHP json_encode() 用于对变量进行 JSON 编码,该函数如果执行成功返回 JSON 数据,否则返回 FALSE . JSON转数组 PHP json_decode() ...
- PHP中把stdClass Object转array的几个方法
方法一: 复制代码代码如下: //PHP stdClass Object转array function object_array($array) { if(is_object($array)) { $ ...
- stdClass Object 数据的处理
stdClass Object 数据的处理 在调用接口的时候往往返回的是 stdClass Object 类型的数据,我们在取数据值的时候就阔以直接使用对象->属性值的方式操作值 $ret = ...
随机推荐
- ubuntu下aircrack-ng的wifi破解
首先安装aircrack-ng,apt-get install aircrack-ng. 然后打开shell,输入airmon-ng start wlan0. 输入airodump-ng mon0. ...
- ### C++总结-[类成员函数]
C++类中的常见函数. #@author: gr #@date: 2015-07-23 #@email: forgerui@gmail.com 一.constructor, copy construc ...
- Python 问题集
1.问题:打开Python的IDLE(集成开发环境/Integrated DeveLopment Environment) 然后在Python的shell中做如下动作时: >>>py ...
- 用VIM写作
Write in VIm 1.Writing in Vim by Dr. Bunsen
- NSDate与 NSString 、long long类型的相互转化
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3560280.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- Poj 2159 / OpenJudge 2159 Ancient Cipher
1.链接地址: http://poj.org/problem?id=2159 http://bailian.openjudge.cn/practice/2159 2.题目: Ancient Ciphe ...
- c#中多线程访问winform控件的若干问题
我们在做winform应用的时候,大部分情况下都会碰到使用多线程控制界面上控件信息的问题.然而我们并不能用传统方法来解决这个问题,下面我将详细的介绍. 首先来看传统方法: public partial ...
- php中 __autoload函数 自动加载类文件机制
魔术函数,自动加载机制function __autoload($class_name) { //自动传递的是类名$path = str_replace('_', '/', $class_name) ...
- linux之vim编辑器
Vi简介1. Vi是一种广泛存在于各种UNIX和Linux系统中的文本编辑程序.2. Vi不是排版程序,只是一个纯粹的文本编辑程序.3. Vi是全屏幕文本编辑器,它没有菜单,只有命令.4. Vi不是基 ...
- git reflog 和git log :no branch git 提交方式
git reflog 和git log的区别,外加git cherry-pick的一种用法 git reflog 可以查看所有分支的所有操作记录(包括(包括commit和reset的操作),包括已经被 ...