PHP“Cannot use object of type stdClass as array”
2015-06-03 10:15
771 查看
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 。
错误:
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 。
相关文章推荐
- [iOS] performSelector:withObject:afterDelay:调用没有作用的问题及解决方法
- object_getClassName swift得到类名
- Could not import Objective-C header
- PHP Object对象转换为Array数组
- a different object with the same identifier value was already associated with异常解决
- Objective-C 基础教程 正式协议
- Compare .NET Objects对象比较组件
- deleted object would be re-saved by cascade (remove deleted object from associations): [com.bp.bean
- modelsim No objects found matching '/t/*' Xilinx IP核
- [Objective-C 面试简要笔记]
- Notification Centers 通知中心
- 用Visual Studio将Objective-C编译为C++
- 强大的 Android 属性动画 ObjectAnimator
- java线程同步:使用Object的wait,notify,notifyAll做线程调度
- Object的Wait() 和notify()方法使用
- Software-artifact Infrastructure Repository 学习笔记 2:C Object Handbooky
- QMetaObject分析
- ELF relocatable object file && ELF Executable object file && Run-time Memory Image 的结构
- Objective-C编码规范:26个方面解决iOS开发问题
- XPCOM (Cross Platform Component Object Model)