导读:项目开发,经常栏目要做到无限极分类,几种方法 PHP无限极分类的几种方法
复制代码 代码如下: namespace Util; 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,$name='child',$pid=0){ $arr =array(); foreach ($cate as $v) { if($v['pid'] == $pid){ $v[$name] = self::unlimitedForlayer($cate,$name,$v['id']); $arr[] = $v; } } return $arr; } //传递子分类PID 返回所有父级分类 static public function getParents($cate,$pid){ $arr=array(); foreach ($cate as $v) { if($v['id'] == $pid){ $arr[] = $v; $arr =array_merge(self::getParents($cate,$v['pid']), $arr); } } return $arr; } //传递父级分类ID,返回子分类ID static public function getChildsId($cate,$id){ $arr =array(); foreach ($cate as $v) { if($v['pid'] == $id){ $arr[] = $v['id']; $arr = array_merge($arr,self::getChildsId($cate,$v['id'])); } } return $arr; }
//传递父级分类ID 返回所有子分类 static public function getChilds($cate,$id){ $arr =array(); foreach ($cate as $v) { if($v['pid'] == $id){ $arr[] = $v; $arr =array_merge($arr,self::getChilds($cate,$v['id'])); } } return $arr;
} }
?>
转自【B5教程网】:http://www.bcty365.com/content-10-1097-1.html
|
请发表评论