PHP Object 转 Array,Json 转 Array
2016-07-14 10:51
886 查看
object 转 array
如果是个 json字符串 的话,可直接通过 json_decode 函数将字符串转换成 object 或 array 。
json_decode 官方手册
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
json_decode — 接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
mixed json_decode ( string $json [, bool $assoc ] )
参数:
json 待解码的 json string 格式的字符串。
assoc 当该参数为 TRUE 时,将返回 array 而非 object 。
通常是用它来转换成PHP数组,如 $arr = json_decode($josnStr, true);
/** * object 转 array */ function object_to_array($obj){ $_arr = is_object($obj)? get_object_vars($obj) : $obj; foreach ($_arr as $key => $val) { $val = (is_array($val)) || is_object($val) ? object_to_array($val) : $val; $arr[$key] = $val; } return $arr; }
如果是个 json字符串 的话,可直接通过 json_decode 函数将字符串转换成 object 或 array 。
json_decode 官方手册
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
json_decode — 接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
mixed json_decode ( string $json [, bool $assoc ] )
参数:
json 待解码的 json string 格式的字符串。
assoc 当该参数为 TRUE 时,将返回 array 而非 object 。
通常是用它来转换成PHP数组,如 $arr = json_decode($josnStr, true);
相关文章推荐
- JSONObject,JSONArray取值
- ScriptableObject
- Objective-C Posing
- Objective-C是动态语言吗?为什么?
- 深入Objective-C的动态特性(基于runtime实现)
- JAVA源码剖析之---Object类(三)---toString,wait,notify,notifyAll,finalize方法的使用
- iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式
- React Native ListView的Item设置点击事件时null is not an object
- IOS-usr/include-objc-NSObject.h-ARC与非ARC
- 异常
- 【Git】Git hangs while unpacking objects (Windows)
- wait()、notify()和notifyAll()是 Object类 中的方法
- 在JAVA中使用JSONObject生成json
- [精通Objective-C]归档和序列化
- 补间动画,属性动画实现购物车添加动画
- repo sync 出错:does not point to a valid object
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- java.lang.IllegalStateException: attempt to re-open an already-closed object
- Objective-C错误码
- js 非构造函数的继承 object()方法