The content of this article is about how to use recursion to achieve infinite grading (code examples) under the TP5 framework. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
I use the tp5 framework. Since many parts of the project use infinite grading, I directly create a catetree package in extend and then create a class Catetee.php, which can be directly referenced in the controller. That’s it
<?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); } }
Related recommendations:
PHP implements stepless recursive classification (ThinkPHP framework), stepless thinkphp
ThinkPHP implements recursive stepless classification with less code, thinkphp recursive
The above is the detailed content of How to use recursion to achieve infinite grading under the TP5 framework (code example). For more information, please follow other related articles on the PHP Chinese website!