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的深度根本没有显示.
任何人都可以向我解释我不理解的东西吗?
| 归档时间: |
|
| 查看次数: |
7407 次 |
| 最近记录: |