- //返回所有的叶子节点
- public function scanNodeOfTree($result,&$array=array(),$parentId=0,$lv=0){
- static $i=0;
- if((bool)$result){
- foreach($result as $value){
- if($value['ParentId']==$parentId){
- $value['lv']=$lv;
- $array[$i]=$value;
- $i++;
- $lv++;
- $this->scanNodeOfTree($result,$array,$value['ID'],$lv--);
- }
- }
- }
- }
- //返回所有的上级节点
- 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;
- }
- //该片段来自于http://www.codesnippet.cn/detail/140520133356.html
来源: http://www.codesnippet.cn/detail/140520133356.html