首頁 > 後端開發 > php教程 > php 无限级分类函数_PHP教程

php 无限级分类函数_PHP教程

WBOY
發布: 2016-07-20 11:07:34
原創
737 人瀏覽過

 

ar=array(0=>array('name'=>'食物','id'=>1,'pid'=>0), 

02               1=>array('name'=>'植物','id'=>2,'pid'=>0), 

03               3=>array('name'=>'白菜','id'=>3,'pid'=>1), 

04               4=>array('name'=>'树','id'=>4,'pid'=>2), 

05               5=>array('name'=>'苹果','id'=>5,'pid'=>3), 

06               6=>array('name'=>'松树','id'=>6,'pid'=>4), 

07               7=>array('name'=>'饮料','id'=>7,'pid'=>1), 

08               8=>array('name'=>'测试1','id'=>8,'pid'=>7), 

09               9=>array('name'=>'测试2','id'=>9,'pid'=>8), 

10               10=>array('name'=>'人民','id'=>10,'pid'=>9), 

11               11=>array('name'=>'小米','id'=>11,'pid'=>0), 

12               ); 

13   

14         echo '

'; 

15         tre($ar,0); 

16         echo '

'; 

17   

18         function tre($tree,$id) 

19         { 

20                 $str = ''; 

21                 $strid = ''; 

22                 foreach ($tree as $key => $item) 

23                 { 

24                         if ($item['pid'] == $id) { 

25                                 $i = 0; 

26                                 $i = sonNum($tree,$item['id']); 

27                                 if ($i == 1 || $i == 0) { 

28                                         echo '

'; 

29                                 } else { 

30                                         echo '

';         

31                                 } 

32                                 //echo '

'; 

33                                 echo $item['name']; 

34                                 echo '

'; 

35                                 tre($tree,$item['id']); 

36                         } 

37                 } 

38                 //tre($tree,$strid); 

39         } 

40           

41         function sonNum($tre,$sum,$totale=1) 

42         { 

43                 foreach ($tre as $key => $value) 

44                 { 

45                         if ($value['pid'] == $sum){ 

46                                 $totale += sonNum($tre,$value['id'],1); 

47                         } 

48                 } 

49                 return $totale; 

50         }


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444952.htmlTechArticlear=array(0=array('name'='食物','id'=1,'pid'=0), 02 1=array('name'='植物','id'=2,'pid'=0), 03 3=array('name'='白菜','id'=3,'pid'=1), 04 4=array('name'='树','id'=4,'pid'=2), 0...
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板