首页 > Web开发 > 详细

PHP“Cannot use object of type stdClass as array”

时间:2015-06-03 11:25:57      阅读:331      评论:0      收藏:0      [点我收藏+]

php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误 

错误: 
Cannot use object of type stdClass as array 

产生原因: 
+展开 
-PHP 
    $res = json_decode($res); 

    $res[‘key‘]; 
//把 json_decode() 后的对象当作数组使用。 


解决方法(2种): 
1、使用 json_decode($d, true)。就是使json_decode 的第二个变量设置为 true。 
2、json_decode($res) 返回的是一个对象, 不可以使用 $res[‘key‘] 进行访问, 换成 $res->key 就可以了。 

mixed json_decode ( string $json [, bool $assoc ] ) 
接受一个 JSON 格式的字符串并且把它转换为 PHP 变量 

参数 

json 
待解码的 json string 格式的字符串。 

assoc 
当该参数为 TRUE 时,将返回 array 而非 object 。

PHP“Cannot use object of type stdClass as array”

原文:http://www.cnblogs.com/mjyblog/p/4548438.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!