ThinkPHP无限级分类查找子孙树代码分析

  • 2,854

ThinkPHP无限级分类查找子孙树代码分析

$this->assign('gettree',$catModel->gettree());
<?php
namespace Admin\Model;
use Think\Model;

class CatModel extends Model{
    public function gettree($p = 0 ,$lv=0){
        $t = array();
        foreach($this->select() as $k=>$v){
        	if($v['parent_id'] == $p){
                $v['lv'] = $lv;
                $t[] = $v;
                //检查
                $t = array_merge($t,$this->gettree($v['cat_id'],$lv+1));
                
            }
        }
        return $t;
    }


}

ThinkPHP无限级分类查找子孙树代码分析

array (size=11)
  0 => 
    array (size=5)
      'cat_id' => string '1' (length=1)
      'cat_name' => string '手机类型' (length=12)
      'intro' => string '手机类型' (length=12)
      'parent_id' => string '0' (length=1)
      'lv' => int 0
  1 => 
    array (size=5)
      'cat_id' => string '2' (length=1)
      'cat_name' => string '小米手机' (length=12)
      'intro' => string '小米手机' (length=12)
      'parent_id' => string '1' (length=1)
      'lv' => int 1
  2 => 
    array (size=5)
      'cat_id' => string '5' (length=1)
      'cat_name' => string '小米5s' (length=8)
      'intro' => string '小米5s' (length=8)
      'parent_id' => string '2' (length=1)
      'lv' => int 2
  3 => 
    array (size=5)
      'cat_id' => string '6' (length=1)
      'cat_name' => string '小米6s' (length=8)
      'intro' => string '小米6s' (length=8)
      'parent_id' => string '2' (length=1)
      'lv' => int 2
  4 => 
    array (size=5)
      'cat_id' => string '3' (length=1)
      'cat_name' => string '华为手机' (length=12)
      'intro' => string '华为手机' (length=12)
      'parent_id' => string '1' (length=1)
      'lv' => int 1
  5 => 
    array (size=5)
      'cat_id' => string '7' (length=1)
      'cat_name' => string '华为一代' (length=12)
      'intro' => string '华为一代' (length=12)
      'parent_id' => string '3' (length=1)
      'lv' => int 2
  6 => 
    array (size=5)
      'cat_id' => string '11' (length=2)
      'cat_name' => string '华为一代1s' (length=14)
      'intro' => string '华为一代1s' (length=14)
      'parent_id' => string '7' (length=1)
      'lv' => int 3
  7 => 
    array (size=5)
      'cat_id' => string '8' (length=1)
      'cat_name' => string '华为二代' (length=12)
      'intro' => string '华为二代' (length=12)
      'parent_id' => string '3' (length=1)
      'lv' => int 2
  8 => 
    array (size=5)
      'cat_id' => string '4' (length=1)
      'cat_name' => string '魅族手机' (length=12)
      'intro' => string '魅族手机' (length=12)
      'parent_id' => string '1' (length=1)
      'lv' => int 1
  9 => 
    array (size=5)
      'cat_id' => string '9' (length=1)
      'cat_name' => string '魅族5' (length=7)
      'intro' => string '魅族5' (length=7)
      'parent_id' => string '4' (length=1)
      'lv' => int 2
  10 => 
    array (size=5)
      'cat_id' => string '10' (length=2)
      'cat_name' => string '魅族6' (length=7)
      'intro' => string '魅族6' (length=7)
      'parent_id' => string '4' (length=1)
      'lv' => int 2

 

weinxin
我的微信
这是我的微信扫一扫
开拓者博主
  • 本文由 发表于 2017年2月14日13:20:43
  • 转载请务必保留本文链接:https://www.150643.com/432.html
匿名

发表评论

匿名网友 填写信息

评论:7   其中:访客  7   博主  0
    • 小刘 小刘 2

      总结的非常好,大赞!

      • 马超金博客 马超金博客 1

        感谢分享

        • PHP程序员雷雪松 PHP程序员雷雪松 2

          学习学习!!!太实用的代码了。