我有一个在树形结构中打印类别的函数。我需要找到每个类别有多少个子类别。
一个例子,括号中给出了子类别计数:
a (3)
-a1 (0)
-a2 (1)
-a21 (0)
b (3)
-b1 (2)
-b11 (1)
-b111 (0)
以下函数打印结构,但如何计算?
$parent
- root id
$category
- 所有ID的列表
function buildCategory($parent, $category) {
$html = "";
global $i;
if (isset($category['ereferencecodes'][$parent])) {
$html .= "<ul>\n";
foreach ($category['ereferencecodes'][$parent] as $cat_id) {
if (!isset($category['ereferencecodes'][$cat_id])) {
$html .= "<li>\n" . $category['referencecodes'][$cat_id]['userName']."(".$category['referencecodes'][$cat_id]['userMobileno'].")". "</a>\n</li> \n"; $i++;}
if (isset($category['ereferencecodes'][$cat_id])) {
$html .= "<li>\n" . $category['referencecodes'][$cat_id]['userName'] . "(".$category['referencecodes'][$cat_id]['userMobileno'].")"."</a> \n";
$result= buildCategory($cat_id, $category);
$html.=$result[0];
$html .= "</li> \n"; $i++;
}
}
//$i =sizeof($category)+1;
$html .= "</ul> \n";
}
//return array($html,$i);
$result = array($html,$i);
return $result;
}