我用的是tp5框架,由于项目多处用无限分级,所以我在extend里面直接创建一个catetree包再创建一个类Catetee.php,就可以在控制器里面直接引用就可以了
<?php namespace catetree; class Catetree { public function catetree($cateRes){ return $this->sort($cateRes); } public function sort($cateRes,$pid=0,$level=0){ static $arr=array(); foreach ($cateRes as $k => $v) { if($v['pid']==$pid){ $v['level']=$level; $arr[]=$v; $this->sort($cateRes,$v['id'],$level+1); } } return $arr; } //获取子栏目id public function childrenids($cateid,$obj){ $data=$obj->field('id,pid')->select(); return $this->_childrenids($data,$cateid,TRUE); } private function _childrenids($data,$cateid,$clear=FALSE){ static $arr=array(); if($clear){ $arr=array(); } foreach ($data as $k => $v) { if($v['pid']==$cateid){ $arr[]=$v['id']; $this->_childrenids($data,$v['id']); } } return $arr; } //处理栏目排序 public function cateSort($data,$obj){ foreach ($data as $k => $v) { $obj->update(['id'=>$k,'sort'=>$v]); } } //处理批量删除 public function pdel($cateids){ foreach ($cateids as $k => $v) { $childrenidsarr[]=$this->childrenids($v); $childrenidsarr[]=(int)$v; } $_childrenidsarr=array(); foreach ($childrenidsarr as $k => $v) { if(is_array($v)){ foreach ($v as $k1 => $v1) { $_childrenidsarr[]=$v1; } }else{ $_childrenidsarr[]=$v; } } $_childrenidsarr=array_unique($_childrenidsarr); $this::destroy($_childrenidsarr); } }
相关推荐:
PHP实现无级递归分类(ThinkPHP框架),无级thinkphp
ThinkPHP实现递归无级分类代码少,thinkphp递归
以上就是TP5框架下如何用递归实现无限分级(代码示例)的详细内容,更多请关注php中文网其它相关文章!
……