无极分类之子孙树、父辈树
2015-08-29 23:47
417 查看
无极分类之子孙树、父辈树
<?php function subTree($arr, $id=0, $depth=0) { static $tree = array(); foreach ($arr as $key=>$value) { if($value['father_id'] == $id) { $value['depth'] = $depth; $tree[] = $value; subTree($arr, $value['id'], $depth+1); //tree($arr, $value['id'],$depth+1); } } return $tree; } function fatherTree($arr,$id) { static $list = array(); foreach($arr as $v) { if($v['id'] == $id) { fatherTree($arr,$v['father_id']); $list[] = $v; } } return $list; } $arr = [ ['id'=>1,'name'=>'news','father_id'=>0], ['id'=>2,'name'=>'article','father_id'=>0], ['id'=>3,'name'=>'local news','father_id'=>1], ['id'=>4,'name'=>'good article','father_id'=>2], ['id'=>5,'name'=>'bad article','father_id'=>2], ['id'=>6,'name'=>'sea','father_id'=>4], ]; $tree = subTree($arr); foreach($tree as $v){ echo str_repeat('----',$v['depth']).$v['name'].$v['depth'].'<br>'; } $list = fatherTree($arr,6); foreach ($list as $key => $value) { echo $value['name'].'>>'; }
相关文章推荐
- 【MFC】根据进程名结束进程学习
- 【hdu4009】【建模】【有向图的最小生成树】
- Cas 数据源之Mysql
- 无法解析的外部符号 __imp__curl_easy_init
- java集合
- 2015/8/29 Python基础(3):数值
- 拓展欧几里得 专题
- Java Servlet完全教程
- 2015惠普测试4班,加油!Fighting~
- Drawable的setBounds方法
- 在调用Qt库来实现功能过程中的一些总结
- 用ping查看计算机的关于网卡,是否接入路由,是否接入Internet
- 机器学习算法之: 逻辑回归 logistic regression (LR)
- C语言中运算符及其优先级
- jQueryMobile的组件之栅格系统(grid)
- 区分html与css中的属性
- hdoj 1049 Climbing Worm【贪心】
- 虚拟化
- Android中常用的工具类
- [leetcode] 125.Valid Palindrome