PHP 不用递归实现无限极分类
无限极分类实现
数据表
CREATE TABLE `zy_category` ( `cat_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', `pid` int(10) NOT NULL COMMENT '父分类', `catename` varchar(50) NOT NULL COMMENT '分类名', `dirname` varchar(255) DEFAULT NULL COMMENT '分类目录', `keywords` varchar(255) DEFAULT NULL COMMENT '关键词', `description` varchar(255) DEFAULT NULL COMMENT '描述', `sort` tinyint(10) NOT NULL DEFAULT '0' COMMENT '排序', `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态 0开启 1关闭', PRIMARY KEY (`cat_id`) USING BTREE ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COMMENT='分类表';
PHP代码
/** * 一维数据数组生成数据树 * @param array $list 数据列表 * @param string $id 父ID Key * @param string $pid ID Key * @param string $son 定义子数据Key * @return Collection */ public static function arr2tree($list, $id = 'id', $pid = 'pid', $son = 'sub') { list($tree, $map) = [[], []]; foreach ($list as $item) { $map[$item[$id]] = $item; } foreach ($list as $item) { if (isset($item[$pid]) && isset($map[$item[$pid]])) { $map[$item[$pid]][$son][] = &$map[$item[$id]]; } else { $tree[] = &$map[$item[$id]]; } } unset($map); return $tree; } /** * 一维数据数组生成数据树 * @param array $list 数据列表 * @param string $id ID Key * @param string $pid 父ID Key * @param string $path * @param string $ppath * @return array */ public static function arr2table($list, $id = 'id', $pid = 'pid', $path = 'path', $ppath = '') { $tree = []; foreach (self::arr2tree($list, $id, $pid) as $attr) { $attr[$path] = "{$ppath}-{$attr[$id]}"; $attr['sub'] = isset($attr['sub']) ? $attr['sub'] : []; $attr['spt'] = substr_count($ppath, '-'); $attr['spl'] = str_repeat(" ├ ", $attr['spt']); $sub = $attr['sub']; unset($attr['sub']); $tree[] = $attr; if (!empty($sub)) { $tree = array_merge($tree, self::arr2table($sub, $id, $pid, $path, $attr[$path])); } } return $tree; }
输出示例:arr2tree
Array ( [0] => Array ( [cat_id] => 1 [pid] => 0 [catename] => Javascript前端 [dirname] => javascript [keywords] => Javascript前端,Javascript,HTML5+CSS3,jQuery,vue.js,Bootstrap,Ajax,Nodejs [description] => Javascript前端栏目发布由站长精心撰写的有关WEB前端开发方面的技术文章,探讨最新最流行的Javascript前端技术,提供在线演示demo和源码下载,最实用强大的WEB技术分享就在www.helloweba.net [sort] => 0 [status] => 0 ) [1] => Array ( [cat_id] => 2 [pid] => 0 [catename] => PHP后端 [dirname] => php [keywords] => PHP,PHP7,PHP后端,PHP教程,Swoole,Laravel,ThinkPHP,全栈,PHP实例,PHP源码下载 [description] => PHP后端栏目发布由站长精心撰写的有关WEB后端开发方面的技术文章,探讨最新最流行的PHP后端技术,提供在线演示demo和源码下载,最实用强大的WEB技术分享就在www.helloweba.net [sort] => 0 [status] => 0 ) [2] => Array ( [cat_id] => 3 [pid] => 0 [catename] => HTML前端 [dirname] => html [keywords] => [description] => [sort] => 0 [status] => 0 [sub] => Array ( [0] => Array ( [cat_id] => 4 [pid] => 3 [catename] => CSS [dirname] => css [keywords] => [description] => [sort] => 0 [status] => 0 [sub] => Array ( [0] => Array ( [cat_id] => 7 [pid] => 4 [catename] => 测试分类 [dirname] => test [keywords] => [description] => [sort] => 0 [status] => 0 ) ) ) [1] => Array ( [cat_id] => 5 [pid] => 3 [catename] => Node.js [dirname] => nodejs [keywords] => [description] => [sort] => 0 [status] => 0 ) ) ) [3] => Array ( [cat_id] => 6 [pid] => 0 [catename] => 其他编程 [dirname] => other [keywords] => [description] => [sort] => 0 [status] => 0 ) )