💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 无限极分类 [TOC] 递归是程序调用自身的编程技巧。 * 使用静态变量存储递归 ``` function deeploop() { static $i = 1; echo $i; $i++; if ($i <= 10) { deeploop(); } } deeploop(); ``` * 使用 值引用 的形式 ``` function deeploop2(&$i = 1) { echo $i; $i++; if ($i <= 10) { deeploop2($i); } } deeploop2(); ``` * 使用 global 全局变量的形式 ``` $i = 1; function deeploop3() { global $i; echo $i; $i++; if ($i <= 10) { deeploop3(); } } deeploop3(); ``` ## 递归无限极分类原理 每一个分类都需要记录它的父级id,当为顶级分类时,父级id为0。这样无论哪个分类,都可以通过父级id一层层的去查明它所有的父级,以便清楚知道它所属何种分类,层级深度为多少。 * 表和数据准备 ``` CREATE TABLE categories ( `id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, `parent_id` TINYINT UNSIGNED NOT NULL COMMENT '上级分类', `cat_name` VARCHAR(12) NOT NULL DEFAULT '' COMMENT '栏目名称', `order` TINYINT NOT NULL DEFAULT 100 COMMENT '排序', `created_at` INT UNSIGNED NOT NULL COMMENT '创建时间', PRIMARY KEY (id) )ENGINE=InnoDB CHARSET "utf8" COMMENT '栏目分类表'; INSERT INTO categories VALUES (1,0,'新闻',1,unix_timestamp(now())),(2,0,'图片',2,unix_timestamp(now())),(3,1,'国内新闻',1,unix_timestamp(now())),(4,1,'国际新闻',2,unix_timestamp(now())),(5,3,'北京新闻',1,unix_timestamp(now())),(6,4,'美国新闻',2,unix_timestamp(now())),(7,2,'美女图片',1,unix_timestamp(now())),(8,2,'风景图片',2,unix_timestamp(now())),(9,7,'日韩明星',1,unix_timestamp(now())),(10,9,'日本AV',1,unix_timestamp(now())); ``` ![](https://box.kancloud.cn/f480f87629fc702099ed8c69ddee1847_323x227.png) ## 无限极分类实现 ``` /** * 无限极分类树 getTree($categories) * @param array $data * @param int $parent_id * @param int $level * @return array */ function getTree($data = [], $parent_id = 0, $level = 0) { $tree = []; if ($data && is_array($data)) { foreach ($data as $v) { if ($v['parent_id'] == $parent_id) { $tree[] = [ 'id' => $v['id'], 'level' => $level, 'cat_name' => $v['cat_name'], 'parent_id' => $v['parent_id'], 'children' => getTree($data, $v['id'], $level + 1), ]; } } } return $tree; } /** * 循环获取子孙树 getSubTree($categories) * * @param array $data * @param int $id * @param int $level * @return array */ function getSubTree($data = [], $id = 0, $level = 0) { static $tree = []; foreach ($data as $key => $value) { if ($value['parent_id'] == $id) { $value['laravel'] = $level; $tree[] = $value; getSubTree($data, $value['id'], $level + 1); } } return $tree; } /** * 递归获取子孙树 getSubTree2($categories, 1) * * @param array $data * @param int $parent_id * @param int $level * @return array */ function getSubTree2($data = [], $parent_id = 0, $level = 0) { $tree = []; if ($data && is_array($data)) { foreach ($data as $key => $value) { if ($value['parent_id'] == $parent_id) { $value['laravel'] = $level; $tree[] = $value; $tree = array_merge($tree, getSubTree2($data, $value['id'], $level + 1)); } } } return $tree; } ``` ``` /** * 通过pid获取所有上级分类 常用于面包屑导航 getParentsByParentId2($categories, 9) * * * @param array $data * @param $parent_id * @return array */ function getParentsByParentId($data = [], $parent_id) { static $categories = []; if ($data && is_array($data)) { foreach ($data as $item) { if ($item['id'] == $parent_id) { $categories[] = $item; getParentsByParentId($data, $item['parent_id']); } } } return $categories; } function getParentsByParentId2($data = [], $parent_id) { $categories = []; if ($data && is_array($data)) { foreach ($data as $item) { if ($item['id'] == $parent_id) { $categories[] = $item; $categories = array_merge($categories, getParentsByParentId2($data, $item['parent_id'])); } } } return $categories; } ```