如何动态计算类别级别。这是一个PHP数组,这可以在用户更改菜单结构时更改,但目前这是一个结构,我只想知道主要类别,邮件类别和主要类别中的子类别有多少级别& #34;品牌&#34 ;.这是我的数组结构。
[1] => Array
(
[name] => 01e3f447-4b81-11e7-a858-b8763f541038
[nodeInfo] => stdClass Object
(
[idCategory] => 01e3f447-4b81-11e7-a858-b8763f541038
[category] => Brands
[slug] => brands
[description] =>
[image] =>
[parentCategory] =>
[idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038
[date] => 2017-06-07 14:58:31
)
[children] => Array
(
[0] => Array
(
[name] => 0ded5b28-4b81-11e7-a858-b8763f541038
[nodeInfo] => stdClass Object
(
[idCategory] => 0ded5b28-4b81-11e7-a858-b8763f541038
[category] => Apparel
[slug] => apparel
[description] =>
[image] =>
[parentCategory] => 01e3f447-4b81-11e7-a858-b8763f541038
[idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038
[date] => 2017-06-07 14:58:51
)
[children] => Array
(
[0] => Array
(
[name] => 3681265e-4b81-11e7-a858-b8763f541038
[nodeInfo] => stdClass Object
(
[idCategory] => 3681265e-4b81-11e7-a858-b8763f541038
[category] => Adidas
[slug] => adidas
[description] =>
[image] =>
[parentCategory] => 0ded5b28-4b81-11e7-a858-b8763f541038
[idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038
[date] => 2017-06-07 14:59:59
)
[children] =>
)
[1] => Array
(
[name] => 3f211015-4b81-11e7-a858-b8763f541038
[nodeInfo] => stdClass Object
(
[idCategory] => 3f211015-4b81-11e7-a858-b8763f541038
[category] => Columbia
[slug] => columbia
[description] =>
[image] =>
[parentCategory] => 0ded5b28-4b81-11e7-a858-b8763f541038
[idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038
[date] => 2017-06-07 15:00:14
)
[children] =>
)
)
)
[1] => Array
(
[name] => 1988d10a-4b81-11e7-a858-b8763f541038
[nodeInfo] => stdClass Object
(
[idCategory] => 1988d10a-4b81-11e7-a858-b8763f541038
[category] => Footwear
[slug] => footwear
[description] =>
[image] =>
[parentCategory] => 01e3f447-4b81-11e7-a858-b8763f541038
[idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038
[date] => 2017-06-07 14:59:11
)
[children] => Array
(
[0] => Array
(
[name] => 49becbc8-4b81-11e7-a858-b8763f541038
[nodeInfo] => stdClass Object
(
[idCategory] => 49becbc8-4b81-11e7-a858-b8763f541038
[category] => Polo
[slug] => polo
[description] =>
[image] =>
[parentCategory] => 1988d10a-4b81-11e7-a858-b8763f541038
[idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038
[date] => 2017-06-07 15:00:31
)
[children] =>
)
[1] => Array
(
[name] => 4e647bee-4b81-11e7-a858-b8763f541038
[nodeInfo] => stdClass Object
(
[idCategory] => 4e647bee-4b81-11e7-a858-b8763f541038
[category] => Nike
[slug] => nike
[description] =>
[image] =>
[parentCategory] => 1988d10a-4b81-11e7-a858-b8763f541038
[idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038
[date] => 2017-06-07 15:00:39
)
[children] =>
)
)
)
)
)
答案 0 :(得分:1)
你可以递归地执行此操作:
function countChildren($array) {
$children = 0;
if (isset($array["children"]) {
$children = count($array["children"]);
foreach ($array["children"] as $subarray) {
$children += countChildren($subarray);
}
}
return $children;
}
只计算你可以做的水平:
function countChildrenLevels($array) {
$children = 0;
if (isset($array["children"]) {
$sublevels = [];
foreach ($array["children"] as $subarray) {
$sublevels[] = countChildren($subarray);
}
$children = 1 + max($sublevels);
}
return $children;
}