首页 >后端开发 >php教程 >PHP实现无限极分类树的方法

PHP实现无限极分类树的方法

墨辰丷
墨辰丷原创
2018-05-18 10:09:071613浏览

这篇文章主要介绍了PHP实现无限极分类树的方法,结合实例形式简单分析了无限极分类的原理与实现方法,涉及PHP数组遍历与判断相关操作技巧,需要的朋友可以参考下

具体如下:

现在的分类数据库设计基本都是:每一个分类有一个id主键字段,一个pid指向父类的id,这样便可实现无限级分类,取出的数据就是如下的格式:


$arr = array(
  array("id" => 1 , "pid" => 0 , 'cat' => '栏目一'),
  array("id" => 2 , "pid" => 0 , 'cat' => '栏目二'),
  array("id" => 3 , "pid" => 1 , 'cat' => '栏目三'),
  array("id" => 4 , "pid" => 2 , 'cat' => '栏目四'),
  array("id" => 5 , "pid" => 1 , 'cat' => '栏目五'),
  array("id" => 6 , "pid" => 5 , 'cat' => '栏目六'),
  array("id" => 7 , "pid" => 5 , 'cat' => '栏目七'),
  array("id" => 8 , "pid" => 6 , 'cat' => '栏目八'),
  array("id" => 9 , "pid" => 1 , 'cat' => '栏目九'),
  array("id" => 10 , "pid" => 0 , 'cat' => '栏目十'),
  array("id" => 11 , "pid" => 10 , 'cat' => '栏目十一'),
  array("id" => 12 , "pid" => 11 , 'cat' => '栏目十二'),
  array("id" => 13 , "pid" => 2 , 'cat' => '栏目十三'),
  array("id" => 14, "pid" => 13 , 'cat' => '栏目十四')
);


不多说,直接上处理代码:


//生成无限极分类树
function make_tree($arr){
  $refer = array();
  $tree = array();
  foreach($arr as $k => $v){
    $refer[$v['id']] = & $arr[$k]; //创建主键的数组引用
  }
  foreach($arr as $k => $v){
    $pid = $v['pid'];  //获取当前分类的父级id
    if($pid == 0){
      $tree[] = & $arr[$k];  //顶级栏目
    }else{
      if(isset($refer[$pid])){
        $refer[$pid]['subcat'][] = & $arr[$k]; //如果存在父级栏目,则添加进父级栏目的子栏目数组中
      }
    }
  }
  return $tree;
}


测试运行:


$cat = make_tree($arr);
print_r($cat);


运行结果:


Array
(
  [0] => Array
    (
      [id] => 1
      [pid] => 0
      [cat] => 栏目一
      [subcat] => Array
        (
          [0] => Array
            (
              [id] => 3
              [pid] => 1
              [cat] => 栏目三
            )
          [1] => Array
            (
              [id] => 5
              [pid] => 1
              [cat] => 栏目五
              [subcat] => Array
                (
                  [0] => Array
                    (
                      [id] => 6
                      [pid] => 5
                      [cat] => 栏目六
                      [subcat] => Array
                        (
                          [0] => Array
                            (
                              [id] => 8
                              [pid] => 6
                              [cat] => 栏目八
                            )
                        )
                    )
                  [1] => Array
                    (
                      [id] => 7
                      [pid] => 5
                      [cat] => 栏目七
                    )
                )
            )
          [2] => Array
            (
              [id] => 9
              [pid] => 1
              [cat] => 栏目九
            )
        )
    )
  [1] => Array
    (
      [id] => 2
      [pid] => 0
      [cat] => 栏目二
      [subcat] => Array
        (
          [0] => Array
            (
              [id] => 4
              [pid] => 2
              [cat] => 栏目四
            )
          [1] => Array
            (
              [id] => 13
              [pid] => 2
              [cat] => 栏目十三
              [subcat] => Array
                (
                  [0] => Array
                    (
                      [id] => 14
                      [pid] => 13
                      [cat] => 栏目十四
                    )
                )
            )
        )
    )
  [2] => Array
    (
      [id] => 10
      [pid] => 0
      [cat] => 栏目十
      [subcat] => Array
        (
          [0] => Array
            (
              [id] => 11
              [pid] => 10
              [cat] => 栏目十一
              [subcat] => Array
                (
                  [0] => Array
                    (
                      [id] => 12
                      [pid] => 11
                      [cat] => 栏目十二
                    )
                )
            )
        )
    )
)


相关推荐:

如何创建无限极分类树型结构

php无需递归实现无限极分类树

有关无限极分类树型结构简单介绍

以上是PHP实现无限极分类树的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn