这是我的数组(考虑数组的深度未知):
Array
(
[0] => Array
(
[id] => 2
[title] => Cars
[sub_category] => Array
(
[0] => Array
(
[id] => 7
[title] => BMW
)
[3] => Array
(
[id] => 12
[title] => Bugatti
[sub_category] => Array
(
[0] => Array
(
[id] => 11
[title] => Bugatti Veyron
[sub_category] => Array
(
[0] => Array
(
[id] => 9
[title] => Bugatti Veyron Blue
[sub_category] => Array
(
[0] => Array
(
[id] => 6
[title] => Bugatti Veyron Blue ...
)
)
)
)
)
)
)
)
)
[1] => Array
(
[id] => 5
[title] => Countries
[sub_category] => Array
(
[0] => Array
(
[id] => 4
[title] => France
[sub_category] => Array
(
[0] => Array
(
[id] => 3
[title] => Paris
)
)
)
)
)
)
我想知道每件物品的深度。
例如,标题为Bugatti Veyron Blue
的项目的深度级别为4,其子级别的深度级别为5
我尝试了一些事情,但输出错误了#34;
function parse_categories($categories,$depth = 1){
foreach($categories as $category){
if(!empty($category['sub_category']) && is_array($category['sub_category'])){
$depth++;
parse_categories($category['sub_category'],$depth);
}
echo $category['title']." : ".$depth."\n";
}
}
parse_categories($array);
我想要的是什么:
Title | Depth
-------------------------+--------
Cars | 1
BMW | 2
Buggati | 2
Buggati Vetron | 3
Buggati Vetron Blue | 4
Buggati Vetron Blue ... | 5
Countries | 1
France | 2
Paris | 3