三级分类

public function getCategorys() {
      $ret = ['first'=>[], 'second'=>[], 'third'=>[]];
      //获取所有分类数据
      $categorys = collection($this->categoryModel
         ->where(['type'=>'direction', 'status'=>'normal'])
         ->order('weigh', 'DESC')
         ->select())->toArray();
      //获取一级分类,并从$categorys移除
      foreach ($categorys as $k=>$v) {
         if ($v['pid'] == 0) {
            $ret['first'][] = $v;
            unset($categorys[$k]);
         }
      }
      //获取二级分类
      foreach ($ret['first'] as $f) {
         foreach ($categorys as $k=>$v) {
            if ($v['pid'] == $f['id']) {
               $ret['second'][] = $v;
            }
         }
      }
    foreach($ret['second'] as $t){
       foreach($categorys as $k=>$v){
          if ($v['pid']==$t['id']){
             $ret['thrid'][]=$v;
          }
       }
    }
   
      //获取三级分类
//      $thmap['status'] = 1;
//      foreach($ret['second'] as $third_k=>$third_v){
//         $thmap['category_id'] = $third_v['id'];
//         $third[$third_k] = ota($this->directionModel
//            ->where($thmap)
//            ->find());
//      }

//      $ret['third'] = array_filter($third);
      return $ret;
   }

猜你喜欢

转载自blog.csdn.net/qq_42449958/article/details/84255323