***php解析JSON二维数组字符串(json_decode函数第二个参数True和False的区别)
2015-03-05 15:25
711 查看
客户端的请求体中的数据:[{"msg_id": 1, "msg_status": "HAS_READ" }, { "msg_id": 2, "msg_status": "HAS_READ" }] 是一个二维数组字符串
$json_data = json_decode($GLOBALS['HTTP_RAW_POST_DATA'], true); 其实用这一句即可实现JSON二维数组字符串转PHP的二维数组变量,不用自己动态构造二维数组 该函数的第二个参数很重要:不加true会以PHP对象输出, 加true输出PHP数组
/**-------------------------------------------------------------------------------------------------------json_decode函数第二个参数True和False的区别默认值:false,返回object对象Array
* 根据上传的消息ID集合来批量更新消息的状态
*/
public function update_status_batch()
{
//需要更新的数据
/* $data = array(
array(
'msg_id' => 1 ,
'msg_status' => 'HAS_READ'
),
array(
'msg_id' => 2 ,
'msg_status' => 'HAS_READ'
)
);*///返回值默认是JSON对象,当第二个可选参数是TRUE的时候,则返回的是数组;如果是二维数组的JSON字符串,这里也会转换为二维数组的PHP变量
$json_data = json_decode($GLOBALS['HTTP_RAW_POST_DATA'], true);
/* //元素个数
//$item_num = count($json_data);
//定义二维数组
$array = array();
foreach($json_data as $item){
$array_unit = array(
'msg_id' => $item->msg_id,
'msg_status' => $item->msg_status
);
//往二维数组追加元素
array_push($array,$array_unit);
}*///更新,返回值是更新所影响的记录条数
$result = $this->m_user_msg->update_batch($json_data, 'msg_id');
if(!empty($result)){
//如果不为空,就返回成功
$return_data['code']= '100';
$return_data['msg']= '处理成功';
//需要进行字符串转数字处理
$return_data['data']= $result;}else{
$return_data['code']= '400';
$return_data['msg']= '处理失败';
$return_data['data']= $json_data;
}//设置以JSON返回给请求方
header('Content-Type:application/json; charset=utf-8');
//转换为JSON字符串
echo stripslashes(json_encode($return_data, JSON_UNESCAPED_UNICODE)) ;}
(
[0] => stdClass Object
(
[name] => C51790CC-EDEF-4DF7-9CC7-FE8F4DC8138B&ext=JPG
[width] => 200
[height] => 149
)[1] => stdClass Object
(
[name] => 4E6A33AD-D9AE-47CD-9711-E95D9184FBC2&ext=JPG.jpg
[width] => 160
[height] => 160
))为true的情况,返回数组array:Array
(
[0] => Array
(
[name] => C51790CC-EDEF-4DF7-9CC7-FE8F4DC8138B&ext=JPG
[width] => 200
[height] => 149
)[1] => Array
(
[name] => 4E6A33AD-D9AE-47CD-9711-E95D9184FBC2&ext=JPG.jpg
[width] => 160
[height] => 160
))官方解释:
json_decode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)json_decode — 对 JSON 格式的字符串进行编码说明
mixed json_decode ( string$json[, bool
$assoc= false [, int
$depth= 512 [, int
$options= 0 ]]] )
接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
参数
json
待解码的
jsonstring 格式的字符串。This function only works with UTF-8 encoded data.
assoc
当该参数为
TRUE时,将返回 array 而非 object 。
depth
User specified recursion depth.
options
Bitmask of JSON decode options. Currently only
JSON_BIGINT_AS_STRINGis supported (default is to cast large integers as floats)
返回值
Returns the value encoded injsonin appropriate PHP type. Values true, false and null (case-insensitive) are returned as
TRUE,
FALSEand
NULLrespectively.
NULLis returned if the
jsoncannot be decoded or if the encoded data is deeper than the recursion limit.
相关文章推荐
- php json字符串中回车换行浏览器没解析
- php解析http获取的json字符串变量总是空白null
- PHP json_decode函数详细解析
- php中json_decode()和json_encode()的使用方法以及其参数‘true’
- angularjs指令中scope参数 true、false、{} 的区别详解
- PHP 输出json字符串无法解析 以前正常
- ajax+php中出现需要使用eval函数将json中字符串解析成json格式
- [php] 解析JSON字符串
- PHP 解析缺少引号的json字符串
- PHP rtrim第二个参数删除的是一个字符列表而不是整个字符串
- jquery解析php通过ajax传过来的json二维数组对象
- php解析http获取的json字符串变量总是空白null
- PHP json_decode函数无法解析json2.js生成json的问题
- php json_decode() 如果想要强制生成PHP关联数组,json_decode()需要加一个参数true
- 【问题-java-json】josn的发送与接收(全)解析json对象与字符串在js中的区别,以及后端spring该如何接收?
- PHP中遇到BOM、<feff>编码导致json_decode函数无法解析问题
- json_encode的第二个参数解析
- android解析php输出的json格式字符串实例
- php解析http获取的json字符串变量总是空白null
- PHP的CURLOPT_POSTFIELDS参数使用数组和字符串的区别