首页 > 代码库 > 无限极分类之查找子孙树

无限极分类之查找子孙树

 1 <?php 2  3 header(‘content-type:text/html;charset=utf8‘); 4 $area = array( 5 array(‘id‘=>1,‘name‘=>‘安徽‘,‘parent‘=>0), 6 array(‘id‘=>2,‘name‘=>‘海淀‘,‘parent‘=>7), 7 array(‘id‘=>3,‘name‘=>‘濉溪县‘,‘parent‘=>5), 8 array(‘id‘=>4,‘name‘=>‘昌平‘,‘parent‘=>7), 9 array(‘id‘=>5,‘name‘=>‘淮北‘,‘parent‘=>1),10 array(‘id‘=>6,‘name‘=>‘朝阳‘,‘parent‘=>7),11 array(‘id‘=>7,‘name‘=>‘北京‘,‘parent‘=>0),12 array(‘id‘=>8,‘name‘=>‘上地‘,‘parent‘=>2)13 );14 15 131 32 function subtree($arr,$id=0){33      static $subs = array();  //静态属性只指向一个地址,不会每次引用都清空$subs;34 35     foreach($arr as $v){36         if($v[‘parent‘]  == $id){37             $subs[] = $v;38             subtree($arr,$v[‘id‘]);//39         }40     }41     return $subs;42 }43 print_r(subtree($area,0));44 ?>

static 总结

1:修饰类的属性和方法,静态属性,静态方法;

2:延迟绑定;static::method();

3:在函数中、方法中声明静态变量用

无限极分类之查找子孙树