php json格式的字符串,【PHP函数】json_decode() ---- 对 JSON 格式的字符串进行解码

json_decode() --- 对 JSON 格式的字符串进行解码

1、用法:

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

参数说明:

$json: json格式的字符串

$assoc:

值为true: 表示返回数组形式的数据

值为false:表示返回对象形式的数据

默认为false

$depth:指定递归深度。

$options: JSON解码的掩码选项。 现在有两个支持的选项。

第一个是JSON_BIGINT_AS_STRING, 用于将大整数转为字符串而非默认的float类型。

第二个是JSON_OBJECT_AS_ARRAY, 与将assoc设置为 TRUE 有相同的效果。

2、范例:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'

json_decode($json);

object(stdClass)#1 (5) {

["a"] => int(1)

["b"] => int(2)

["c"] => int(3)

["d"] => int(4)

["e"] => int(5)

}

json_decode($json, true);

array(5) {

["a"] => int(1)

["b"] => int(2)

["c"] => int(3)

["d"] => int(4)

["e"] => int(5)

}

3、应用场景:

有返回的数据$modeofpayment,需要对其进行循环输出每条数据。如何实现?

其中:$modeofpayment = [{"id":1,"name":"货到付款"},{"id":2,"name":"支付宝付款"},{"id":3,"name":"微信付款"}];

(1)var_dump()打印结果为string '[{"id":1,"name":"货到付款"},{"id":2,"name":"支付宝付款"},{"id":3,"name":"微信付款"}]'

其数据类型为string

(2)将数据转化为数组形式:json_decode($json, true)。得到结果为:

array (size=3)

0 =>

array (size=2)

'id' => int 1

'name' => string '货到付款' (length=12)

1 =>

array (size=2)

'id' => int 2

'name' => string '支付宝付款' (length=15)

2 =>

array (size=2)

'id' => int 3

'name' => string '微信付款' (length=12)

(3)使用模板引擎循环输出

{foreach name="$offer.modeofpayment" item="vo"}

{$vo.name}

{/foreach}

或者

{volist name="$offer.modeofpayment" id="vo"}

{$vo.name}

{/volist}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值