-
博文分类专栏
- Jquery基础教程
-
- 文章:(15)篇
- 阅读:46569
- shell命令
-
- 文章:(42)篇
- 阅读:154252
- Git教程
-
- 文章:(36)篇
- 阅读:234888
- leetCode刷题
-
- 文章:(76)篇
- 阅读:131877
-
PHP中无限极分类函数的实现详解2016-05-21 17:11 阅读(5374) 评论(0)
在开发程序的时候,有时候,我们需要获取所有栏目(包括一级栏目、二级栏目、三级栏目等等),并按照父子关系形成树型结构。可以采取使用递归或者通过引用方式(php中引用类似C或者C++中的指针,只不过换一种说法而已)。
通过引用方式实现无限极分类
思路:
1.即所有待处理的数据进行包装成下标为主键id(pk)的数组,便于有 pid获取对应的父栏目。
2.对包装的数据进行循环,如果为根节点,则将其引用添加到tree中,否则,将其引用添加到其父类的子元素中。这样虽然 tree中,只是添加了根节点,但是每个根节点如果有子元素,其中包含了子元素的引用。故能形成树型。代码如下
/** * 把返回的数据集转换成Tree * @param array $list 要转换的数据集 * @param string $pk 自增字段(栏目id) * @param string $pid parent标记字段 * @return array * @author dqs <1696232133@qq.com> */function make_tree($list,$pk='id',$pid='pid',$child='_child',$root=0){ $tree=array(); $packData=array(); foreach ($list as $data) { $packData[$data[$pk]] = $data; } foreach ($packData as $key =>$val){ if($val[$pk]==$root){//代表跟节点 $tree[]=& $packData[$key]; }else{ //找到其父类 $packData[$val[$pid]][$child][]=& $packData[$key]; } } return $tree; }
数据结构如下:
执行结果如下
Array( [0] => Array ( [id] => 6 [pid] => 0 [title] => 测试1 [url] => test1 [sort] => 0 [create_time] => 1464076199 [update_time] => 1464076199 [status] => 1 [target] => 0 ) [1] => Array ( [id] => 7 [pid] => 0 [title] => 测试2 [url] => test2 [sort] => 0 [create_time] => 1464076218 [update_time] => 1464076218 [status] => 1 [target] => 0 ) [2] => Array ( [id] => 1 [pid] => 0 [title] => 首页 [url] => Index/index [sort] => 1 [create_time] => 1379475111 [update_time] => 1379923177 [status] => 1 [target] => 0 ) [3] => Array ( [id] => 2 [pid] => 0 [title] => 博客 [url] => Article/index?category=blog [sort] => 2 [create_time] => 1379475131 [update_time] => 1379483713 [status] => 1 [target] => 0 [_child] => Array ( [0] => Array ( [id] => 4 [pid] => 2 [title] => 个人博客 [url] => ownblog [sort] => 0 [create_time] => 1464075558 [update_time] => 1464075558 [status] => 1 [target] => 0 [_child] => Array ( [0] => Array ( [id] => 8 [pid] => 4 [title] => 新浪博客 [url] => sinaBlog [sort] => 0 [create_time] => 1464077276 [update_time] => 1464077276 [status] => 1 [target] => 0 ) ) ) [1] => Array ( [id] => 5 [pid] => 2 [title] => 他人博客 [url] => otherBlog [sort] => 0 [create_time] => 1464075582 [update_time] => 1464075582 [status] => 1 [target] => 0 ) ) ) [4] => Array ( [id] => 3 [pid] => 0 [title] => 官网 [url] => http://www.onethink.cn [sort] => 3 [create_time] => 1379475154 [update_time] => 1387163458 [status] => 1 [target] => 0 ) )
附加OneThink中无限极分类函数
/** * 把返回的数据集转换成Tree * @param array $list 要转换的数据集 * @param string $pid parent标记字段 * @param string $level level标记字段 * @return array * @author 麦当苗儿 <zuojiazi@vip.qq.com> */function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) { // 创建Tree $tree = array(); if(is_array($list)) { // 创建基于主键的数组引用 $refer = array(); foreach ($list as $key => $data) { $refer[$data[$pk]] =& $list[$key]; } foreach ($list as $key => $data) { // 判断是否存在parent $parentId = $data[$pid]; if ($root == $parentId) { $tree[] =& $list[$key]; }else{ if (isset($refer[$parentId])) {