php 無限級分類函數

來源:互聯網
上載者:User

 

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 '<table border="1" width="100%">'; 

15         tre($ar,0); 

16         echo '</table>'; 

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 '<tr><td>'; 

29                                 } else { 

30                                         echo '<tr><td rowspan="'.$i.'">';         

31                                 } 

32                                 //echo '<tr><td>'; 

33                                 echo $item['name']; 

34                                 echo '</td></tr>'; 

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         }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.