PHP“Cannot use object of type stdClass as array”

 php再调用json_decode从字符串对象生成json对象时,若是使用[]操做符取数据,会获得下面的错误

错误:
Cannot use object of type stdClass as array

产生缘由:
php

+展开json

-PHP数组

    $res = json_decode($res);

    $res['key']; //把 json_decode() 后的对象看成数组使用。spa



解决方法(2种):
一、使用 json_decode($d, true)。就是使json_decode 的第二个变量设置为 true。
二、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.code

返回值默认是JSON对象,当第二个可选参数是TRUE的时候,则返回的是数组对象

相关文章
相关标签/搜索