PHP json_decode深度参数不起作用 [英] PHP json_decode depth parameter doesn't work

查看:437
本文介绍了PHP json_decode深度参数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP的json_decode函数具有一个"depth"参数,您可以在其中指定递归的深度.但是下面的代码:

PHP's json_decode function has a "depth" parameter, where you can specify how deep it will recurse. But the following code:

test = array(
    'name' => 'sean',
    'dob' => '12-20',
    'parents' => array(
        'father' => 'tommy',
        'mother' => 'darcy'
    )
);

foreach(range(1, 3) as $depth) {
    echo "-----------------\n depth: $depth\n";
    print_r(json_decode(json_encode($test), true, $depth));
}

产生此输出:

-----------------
 depth: 1
-----------------
 depth: 2
-----------------
 depth: 3
Array
(
    [name] => sean
    [dob] => 12-20
    [parents] => Array
        (
            [father] => tommy
            [mother] => darcy
        )

)

我期望的是,深度为1时显示姓名"和"dob",深度为2时也显示父母.我不明白为什么1或2的深度根本不显示任何内容.

What I would expect is a depth of 1 to show "name" and "dob", and a depth of 2 to show the parents, also. I don't get why a depth of 1 or 2 displays nothing at all.

任何人都可以向我解释我不了解的内容吗?

Can anyone explain to me what I'm not understanding?

推荐答案

文档说明原因.

如果无法解码json,则返回

NULL;如果编码数据的深度大于递归限制,则返回.

NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

这篇关于PHP json_decode深度参数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆