最新消息:

php json_encode “Cannot use object of type stdClass as array” 的解决方法

资讯 feifei 2393浏览

错误:

Cannot use object of type stdClass as array

产生原因:

$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  就可以了。

参考手册:json_decode

Return Values:Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.

转载请注明:飞飞的个人网站 » php json_encode “Cannot use object of type stdClass as array” 的解决方法