php 5.2 json decode,***php解析JSON二维数组字符串(json_decode函数第二个参数True和False的区别)...

客户端的请求体中的数据:[{"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数组

/**

* 根据上传的消息ID集合来批量更新消息的状态*/

public functionupdate_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)) ;

}

-------------------------------------------------------------------------------------------------------

json_decode函数第二个参数True和False的区别

默认值:false,返回object对象

Array

(

[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

待解码的 json string 格式的字符串。

This function only works with UTF-8 encoded data.

assoc

当该参数为 TRUE 时,将返回

depth

User specified recursion depth.

options

Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats)

返回值

Returns the value encoded in json in appropriate PHP type. Values true, false and null (case-insensitive) are returned as TRUE, FALSE andNULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.如何联系我:【万里虎】www.bravetiger.cn

【QQ】3396726884 (咨询问题100元起,帮助解决问题500元起)

【博客】http://www.cnblogs.com/kenshinobiy/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值