PHP 递归读取无限级分类
1 /** 2 * 【获取第一级分类】 3 * 20170829 4 * 5 * @return array 6 */ 7 public function getCateList(){ 8 $this->catelog = []; 9 //获取第一级分类 10 $rst = $this->find()->where(['is_root'=>0, 'status' => 10])->asArray()->all(); 11 12 if($rst){ 13 foreach ($rst as $row){ 14 $this->catelog[] = [ 15 'id' => $row['id_cate'], 16 'catename' => $row['catename'], 17 'catename_en' => $row['catename_en'], 18 'cover' => $row['cover'], 19 'depath' => 0, 20 'is_root' => $row['is_root'], 21 ]; 22 //获取子分类 23 $this->getChild($row['id_cate'], 1); 24 25 } 26 } 27 28 return $this->catelog; 29 } 30 31 32 /** 33 * 【递归获取所有子分类】 34 * 20170829 35 * 36 * @param $id 37 * @param $depth 38 */ 39 function getChild($id,$depth ){ 40 //global $catalog; 41 42 $rst = $this->find()->where(['is_root' => $id, 'status' => 10])->asArray()->all(); 43 if (count($rst) > 0) { 44 foreach ($rst as $row){ 45 $this->catelog[] = [ 46 'id' => $row['id_cate'], 47 'catename' => $row['catename'], 48 'catename_en' => $row['catename_en'], 49 'cover' => $row['cover'], 50 'depath' => $depth, 51 'is_root' => $row['is_root'], 52 ]; 53 $this->getChild($row['id_cate'], 2); 54 } 55 } 56 57 }
生命不息,学习不止