PHP的json_decode的深度参数不起作用

Sea*_*ean 6 php json

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

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));
}
Run Code Online (Sandbox Code Playgroud)

生成此输出:

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

)
Run Code Online (Sandbox Code Playgroud)

我期望的是深度为1以显示"名称"和"dob",深度为2以显示父母.我不明白为什么1或2的深度根本没有显示.

任何人都可以向我解释我不理解的东西吗?

Exp*_*lls 8

文档说明了原因.

如果无法解码json或编码数据深于递归限制,则返回NULL .