无限分类的
<?php
namespace Common\COM;
Class Category {
Static Public function catesort($cate, $html = ' --', $pid = 0, $level = 0){
$arr = array();
foreach($cate as $v){
if ($v['pid'] == $pid)//如果是一级栏目,那么开始便利子栏目
{
$v['level'] = $level +1;//如果是一级栏目,那么他的下级栏目就是level+1
$v['html'] = str_repeat($html, $level);//替换掉他
$arr[] = $v;//吧这个值付给一个数组
$arr = array_merge($arr, self::catesort($cate, $html, $v['id'], $level + 1));
//调用自己方法,并且根这个数组合并
}
}
return $arr;
}
Static Public function catesortforlayer($cate, $name = 'child', $pid = 0){
$arr = array();
foreach($cate as $v){
if($v['pid'] == $pid){
$v[$name] = self::catesortforlayer($cate, $name, $v['id']);
$arr[] = $v;
}
}
return $arr;
}
Static Public function getParents ($cate, $id){
$arr = array();
foreach($cate as $v){
if($v['id'] == $id){
$arr = array_merge($arr, self::getParents($cate, $v['pid']));
$arr[] = $v;
}
}
return $arr;
}
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;
}
Static Public function getChilds($cate, $pid){
$arr = array();
foreach($cate as $v){
if($v['pid'] == $pid){
$arr[] = $v;
$arr = array_merge($arr, self::getChildsId($cate, $v['id']));
}
}
return $arr;
}
}
?>