php 无限极栏目 Tree 树格式 层级显示 【列表记录按顺序缩进显示】

如下图:

  1 <?php
  2 
  3 /**
  4  * 栏目记录树
  5  * Author 撑一支船蒿  hly
  6  * 364077286@qq.com
  7  */
  8 class tree
  9 {
 10     /**
 11      * Notes: 递归拿到每个一级栏目的 的子数组
 12      * User: hly
 13      * Date: 2020/1/7 15:27
 14      * @param $category_arr
 15      * @param int $pid
 16      * @param int $level
 17      * @return array
 18      */
 19     public function generateTree($category_arr, $pid = 0, $level = 1, $field = array())
 20     {
 21         //总记录列表
 22         $arr = array();
 23         foreach ($category_arr as & $v) {
 24             if ($v[$field['parentid']] == $pid) {
 25                 //追加字段 记录每个栏目的层级数
 26                 $v['level'] = $level;
 27                 //获取此次循环的下一级记录
 28                 $tem = $this->generateTree($category_arr, $v[$field['categoryid']], $v['level'] + 1, $field);
 29                 $arr[] = $v;
 30                 //按顺序追加到总记录中
 31                 $arr = array_merge($arr, $tem);
 32             }
 33         }
 34         return $arr;
 35     }
 36 
 37     /**
 38      * Notes: 拼接栏目前隔符
 39      * User: hly
 40      * Date: 2020/1/7 15:27
 41      * @param $level  栏目层级数
 42      * @param $is_has_children :是否有子栏目
 43      * @param $is_last 当前栏目是否是同级栏目中的最后一个
 44      * @return string
 45      */
 46     public function generateStr($level, $is_has_children, $is_last)
 47     {
 48         $str = '';
 49         for ($i = 1; $i < $level; $i++) {
 50             if (!$is_has_children) {
 51                 if ($is_last) {
 52                     if ($i == ($level - 1)) {
 53                         $str .= '&nbsp;&nbsp;└─';
 54                     } else {
 55                         $str .= '&nbsp;&nbsp;│';
 56                     }
 57                     //处理一下轮
 58                     continue;
 59                 }
 60             }
 61 
 62             //不满足上面条件的 拼接这个
 63             if ($i == ($level - 1)) {
 64                 $str .= '&nbsp;&nbsp;├─';
 65             } else {
 66                 $str .= '&nbsp;&nbsp;│';
 67             }
 68         }
 69         return $str;
 70     }
 71 
 72     /**
 73      * Notes: 处理 输出格式数据
 74      * User: hly
 75      * Date: 2020/1/7 15:27
 76      * @param $category_arr
 77      * @param $field 定义栏目记录的字段信息
 78      * @return array
 79      */
 80     public function formatTree($category_arr, $field)
 81     {
 82         //获取层级数据
 83         $tree_data = $this->generateTree($category_arr, 0, 1, $field);
 84         //显示顺序结构
 85         //var_dump($tree_data);
 86         //exit();
 87 
 88         //收集所有的栏目的 pid
 89         $pids = array();
 90         //收集每个同级栏目的 最后一个栏目的 cid
 91         $last_cids = array();
 92         foreach ($tree_data as $v) {
 93             $pids[] = $v[$field['parentid']];
 94             $last_cids[$v[$field['parentid']]] = $v[$field['categoryid']];
 95         }
 96 
 97         //打印格式
 98         foreach ($tree_data as $k => $v) {
 99             $is_has_children = in_array($v[$field['categoryid']], $pids) == true ? 1 : 0;
100             $is_last = $last_cids[$v[$field['parentid']]] == $v[$field['categoryid']] ? 1 : 0;
101             //对非第一级栏目 拼接前隔符 $has_children:是否有子栏目  $is_last:当前栏目是否是同级栏目中的最后一个
102             $str = $v['level'] != 1 ? $this->generateStr($v['level'], $is_has_children, $is_last) : '';
103             $str .= $v[$field['name']];
104             $tree_data[$k][$field['name']] = $str;
105         }
106         return $tree_data;
107     }
108 }
109 
110 $tree = new tree();
111 
112 //测试数据
113 $category_arr = [
114     //cid => category_id   pid => parentid
115     ['cid' => 2, 'name' => '一级栏目1', 'pid' => 0],
116     ['cid' => 4, 'name' => '一级栏目2', 'pid' => 0],
117     ['cid' => 5, 'name' => '一级栏目3', 'pid' => 0],
118     ['cid' => 7, 'name' => '二级栏目1', 'pid' => 2],
119     ['cid' => 8, 'name' => '二级栏目2', 'pid' => 2],
120     ['cid' => 9, 'name' => '三级栏目1', 'pid' => 7],
121     ['cid' => 10, 'name' => '二级栏目4', 'pid' => 5],
122     ['cid' => 11, 'name' => '四级栏目1', 'pid' => 9],
123     ['cid' => 12, 'name' => '五级栏目1', 'pid' => 11],
124     ['cid' => 13, 'name' => '六级栏目1', 'pid' => 12],
125     ['cid' => 14, 'name' => '七级栏目1', 'pid' => 13],
126     ['cid' => 15, 'name' => '五级栏目2', 'pid' => 11],
127     ['cid' => 16, 'name' => '二级栏目5', 'pid' => 2],
128     ['cid' => 17, 'name' => '五级栏目3', 'pid' => 11],
129     ['cid' => 18, 'name' => '三级栏目2', 'pid' => 7],
130     ['cid' => 19, 'name' => '四级栏目2', 'pid' => 9],
131     ['cid' => 20, 'name' => '六级栏目2', 'pid' => 12],
132     ['cid' => 21, 'name' => '四级栏目3', 'pid' => 9],
133     ['cid' => 22, 'name' => '四级栏目4', 'pid' => 9]
134 ];
135 $field = ['parentid' => 'pid', 'categoryid' => 'cid', 'name' => 'name'];
136 $data = $tree->formatTree($category_arr, $field);
137 
138 //打印格式
139 echo '<ul>';
140 foreach ($data as $k => $v) {
141     echo '<li>' . $v['name'] . '</li>';
142 }
143 echo '</ul>';

 

 

 

 注意:开发环境还需根据您的场景优化下

  

赞赏码^_^

posted @ 2020-01-03 17:24  撑一支船蒿  阅读(472)  评论(0编辑  收藏  举报