無限極分類類

發表於2019-05-11

<?php
// +----------------------------------------------------------------------
// | GEEKCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 All rights reserved.
// +----------------------------------------------------------------------
// | Author: Marvin(柳英偉)
// +----------------------------------------------------------------------
class Category{
    //    組合一維陣列
    Static Public function unLimitedForLevel($cate,$html='____',$pid=0,$level=0){
        $arr=array();
        foreach($cate as $v){
            if($v['pid']==$pid){
                $v['level']=$level+1;
                $v['html']=str_repeat($html,$level);
                $arr[]=$v;
                $arr=array_merge($arr,self::unLimitedForLevel($cate,$html,$v['id'],$level+1));
            }
        }
        return $arr;
    }
    //組合多維陣列
    Static Public function unLimitedForLayer($cate,$pid=0){
        $arr=array();
        foreach($cate as $v){
            if($v['pid']==$pid){
                $v['cate']=self::unLimitedForLayer($cate,$v['id']);
                $arr[]=$v;
            }
        }
        return $arr;
    }
    //傳遞一個子分類ID返回所有父級分類
    Static Public function getParents($cate,$id){
        $arr=array();
        foreach($cate as $v){
            if($v['id']==$id){
                $arr[]=$v;
                $arr=array_merge(self::getParents($cate,$v['pid']),$arr);
            }
        }
    return $arr;
    }
    //傳遞一個父級分類ID返回所有子級分類
    Static Public function getChildsId($cate,$pid){
        $arr=array();
        foreach ($cate as $v){
            if($v['pid']==$pid){
                $arr[]=$v['id'];
                $arr=array_merge($arr,self::getChildsId($cate,$v['id']));
            }
        }
    return $arr;
    }
}//end
評論(2)

相關文章