php无限极分类
2021-06-11 23:05
标签:alt log com code images new 技术 oba ima 然后我们只需要写一个调用的代码就好 这样看上去是不是很简单? php无限极分类 标签:alt log com code images new 技术 oba ima 原文地址:http://www.cnblogs.com/lujiang/p/7290531.html 1 $arr = array(
2 0=>array(
3 ‘cid‘=>1,
4 ‘pid‘=>0,
5 ‘name‘=>‘亚洲‘,
6 ),
7 1=>array(
8 ‘cid‘=>2,
9 ‘pid‘=>0,
10 ‘name‘=>‘北美洲‘,
11 ),
12 2=>array(
13 ‘cid‘=>3,
14 ‘pid‘=>1,
15 ‘name‘=>‘中国‘,
16 ),
17 3=>array(
18 ‘cid‘=>4,
19 ‘pid‘=>2,
20 ‘name‘=>‘美国‘,
21 ),
22 4=>array(
23 ‘cid‘=>5,
24 ‘pid‘=>3,
25 ‘name‘=>‘北京‘,
26 ),
27 5=>array(
28 ‘cid‘=>6,
29 ‘pid‘=>3,
30 ‘name‘=>‘河北‘,
31 ),
32 6=>array(
33 ‘cid‘=>7,
34 ‘pid‘=>5,
35 ‘name‘=>‘东城区‘,
36 ),
37 7=>array(
38 ‘cid‘=>8,
39 ‘pid‘=>5,
40 ‘name‘=>‘海淀区‘,
41 ),
42 );
1 private function GetTree($arr,$pid,$step){
2 global $tree;
3 foreach($arr as $key=>$val) {
4 if($val[‘pid‘] == $pid) {
5 $flg = str_repeat(‘└―‘,$step);
6 $val[‘name‘] = $flg.$val[‘name‘];
7 $tree[] = $val;
8 $this->GetTree($arr , $val[‘cid‘] ,$step+1);
9 }
10 }
11 return $tree;
12 }
$newarr = $this->GetTree($arr, 0, 0);