本文实例讲述了php可应用于面包屑导航的迭代寻找家谱树实现方法。分享给大家供大家参考。具体实现方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<?php
echo "<pre>" ;
$area = array (
array ( 'id' =>1, 'area' => '北京' , 'pid' =>0),
array ( 'id' =>2, 'area' => '广西' , 'pid' =>0),
array ( 'id' =>3, 'area' => '广东' , 'pid' =>0),
array ( 'id' =>4, 'area' => '福建' , 'pid' =>0),
array ( 'id' =>11, 'area' => '朝阳区' , 'pid' =>1),
array ( 'id' =>12, 'area' => '海淀区' , 'pid' =>1),
array ( 'id' =>21, 'area' => '南宁市' , 'pid' =>2),
array ( 'id' =>45, 'area' => '福州市' , 'pid' =>4),
array ( 'id' =>113, 'area' => '亚运村' , 'pid' =>11),
array ( 'id' =>115, 'area' => '奥运村' , 'pid' =>11),
array ( 'id' =>234, 'area' => '武鸣县' , 'pid' =>21)
);
function familytree( $arr , $id ){
$list = array ();
while ( $id ){
$flag = false;
foreach ( $arr as $v ){
if ( $v [ 'id' ]== $id ){
array_unshift ( $list , $v [ 'area' ]);
$id = $v [ 'pid' ];
$flag = true;
}
}
if (! $flag ){
break ;
}
}
return $list ;
}
print_r(familytree( $area ,113));
?>
|
希望本文所述对大家的php程序设计有所帮助。