php 無限分類父子追溯方法
來源:程序員人生 發布時間:2014-03-16 23:25:13 閱讀次數:3014次
php 無限分類之父級分類和子分類的追溯方法,代碼片段如下:
//返回所有的葉子節點
public function scanNodeOfTree($result,$fid){
$checkexist = false;
for ($i=0; $i<count($result); $i++){
if($fid == $result[$i]['ParentId']){
$checkexist = true;
$arr .= $this->scanNodeOfTree($result,$result[$i]['ID']).',';
}
}
if(!$checkexist){
return $fid;
}
return $arr;
}
//返回所有的上級節點
public function getNodeOfTree($result,$id,$arr){
if($id == 0){
return $arr;
}
foreach ($result as $items){
if($id == $items['ID']){
$arr[] = array($items['CateName'],$items['ID']);
$return = $this->getNodeOfTree($result,$items['ParentId'],$arr);
}
}
return $return;
}