- <?php
- /*
- * 找子栏目
- */
- $area=array(
- array(‘id‘=>1,‘name‘=>‘中国‘,‘parent‘=>0),
- array(‘id‘=>2,‘name‘=>‘广东‘,‘parent‘=>1),
- array(‘id‘=>3,‘name‘=>‘深圳‘,‘parent‘=>2),
- array(‘id‘=>4,‘name‘=>‘湖南‘,‘parent‘=>1),
- array(‘id‘=>5,‘name‘=>‘长沙‘,‘parent‘=>4)
- );
- function findson($arr,$id=0) {
- $son=array();
- foreach ($arr as $value) {
- if ($value[‘parent‘]==$id) {
- $son[]=$value;
- }
- }
- return $son;
- }
- /*
- * 找子孙树
- * @param array 传入的数组
- * @param int 传入的id
- */
- function substree($arr,$id=0,$level=1) {
- static $grandson=array();
- foreach ($arr as $value) {
- if ($value[‘parent‘]==$id) {
- $value[‘level‘]=$level;
- $grandson[]=$value;
- substree($arr,$value[‘id‘],$level+1);
- }
- }
- return $grandson;
- }
- //var_dump( findson($area,1));
- echo "</br>";
- //var_dump(substree($area,2));
- /*
- * 为了让它显示缩进效果
- */
- $tt=substree($area,0,1);
- foreach ($tt as $value) {
- echo str_repeat(‘ ‘, $value["level"]).$value["name"]."</br>";
- }
- ?>
时间: 2024-10-13 12:54:05