PHP“Cannot use object of type stdClass as array” (php在调用json_decode从字符串对象生成json对象时的报错)
2015-02-09 18:02
931 查看
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 就可以了。
参考手册:json_decode
Return Values:Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.
返回值默认是JSON对象,当第二个可选参数是TRUE的时候,则返回的是数组
错误:
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 就可以了。
参考手册:json_decode
Return Values:Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.
返回值默认是JSON对象,当第二个可选参数是TRUE的时候,则返回的是数组
相关文章推荐
- 使用json_decode()转换返回数组或对象时出现:Cannot use object of type stdClass as array
- json_decode(Cannot use object of type stdClass as array)
- PHP JSON出错:Cannot use object of type stdClass as array解决方法
- PHP JSON出错:Cannot use object of type stdClass as array解决方法
- PHP“Cannot use object of type stdClass as array”
- PHP“Cannot use object of type stdClass as array”
- PHP报错:Cannot use object of type stdClass as array
- PHP“Cannot use object of type stdClass as array”
- PHP“Cannot use object of type stdClass as array”
- PHP“Cannot use object of type stdClass as array”
- PHP“Cannot use object of type stdClass as array”
- **PHP错误Cannot use object of type stdClass as array in错误的
- php错误 Fatal error: Cannot use object of type stdClass as array in……
- php “Cannot use object of type stdClass as array” 的解决方法
- PHP错误Cannot use object of type stdClass as array in错误的解决办法
- PHP中遍历stdclass object 报错Fatal error: Cannot use objectof type stdClass as array
- Cannot use object of type stdClass as array
- Cannot use object of type stdClass as array
- Cannot use object of type PHPExcel_RichText as array
- PHP 错误:Cannot use object of type yii\db\Command as array