|
发表于 2013-10-10 09:02:08
|
显示全部楼层
|阅读模式
来自 中国–河北–石家庄
- <?php
- header("Content-type: text/html; charset=utf-8");
- echo '<pre>';
- $area = array(
- array('id'=>1,'name'=>'安徽','parent'=>0),
- array('id'=>2,'name'=>'海淀','parent'=>7),
- array('id'=>3,'name'=>'濉溪县','parent'=>5),
- array('id'=>4,'name'=>'昌平','parent'=>7),
- array('id'=>5,'name'=>'淮北','parent'=>1),
- array('id'=>6,'name'=>'朝阳','parent'=>7),
- array('id'=>7,'name'=>'北京','parent'=>0),
- array('id'=>8,'name'=>'上地','parent'=>2)
- );
- function findson($area,$id=0){
- static $arr = array();
- foreach($area as $v){
- if($v['id'] == $id){
- $arr[] = $v;
- if($v['parent'] != 0){
- //$id = $v['parent'];
- findson($area,$v['parent']);
- }
- }
- }
- return $arr;
- }
- print_r(findson($area,8));
- ?>
复制代码 为什么把26行的“//$id = $v['parent'];”这个屏蔽起来就正常,打开就错误了呢?屏蔽是- Array
- (
- [0] => Array
- (
- [id] => 8
- [name] => 上地
- [parent] => 2
- )
- [1] => Array
- (
- [id] => 2
- [name] => 海淀
- [parent] => 7
- )
- [2] => Array
- (
- [id] => 7
- [name] => 北京
- [parent] => 0
- )
- )
复制代码 打开26行的“$id = $v['parent'];”就是- Array
- (
- [0] => Array
- (
- [id] => 8
- [name] => 上地
- [parent] => 2
- )
- [1] => Array
- (
- [id] => 2
- [name] => 海淀
- [parent] => 7
- )
- [2] => Array
- (
- [id] => 7
- [name] => 北京
- [parent] => 0
- )
- [3] => Array
- (
- [id] => 7
- [name] => 北京
- [parent] => 0
- )
- )
复制代码 为什么会多输出一个北京呢?求大神们解释一下,小弟实在想不明白
|
评分
-
查看全部评分
|