Thinkphp无限分类函数

<?php
 
    Class Category{
 
        //一维数组无限级分类
        Static Public function yiwei($cate,$pid=0){
 
            $arr=array();
            foreach($cate as $v){
                if($v['pid'] == $pid){
                    $arr[]=$v;
                    $arr=array_merge($arr,self::yiwei($cate,$v['id']));
                }
                 
            }
            return $arr;
        }
 
        //二位数组无限级分类
        Static Public function erwei($cate,$pid=0){
            $arr=array();
            foreach($cate as $v){
                if($v['pid'] == $pid){
                     
                    $v['child']=self::erwei($cate,$v['id']);
                    $arr[]=$v;
                }
            }
            return $arr;
        }
 
 
        //通过子级寻找父级
        Static Public function findFather($cate,$id){
            $arr=array();
            foreach($cate as $v){
                if($v['id'] == $id){
                    $arr[]=$v;
                    $arr=array_merge($arr,self::findFather($cate,$v['pid']));
                }
            }
            return $arr;
        }
 
        //通过父级寻找子级
        Static Public function findChild($cate,$pid){
            $arr=array();
            foreach($cate as $v){
                if($v['pid'] == $pid){
                    $arr[]=$v['id'];
                    $arr=array_merge($arr,self::findChild($cate,$v['id']));
                }
            }
            return $arr;
        }
    }
?>

你可能感兴趣的:(Thinkphp无限分类函数)