Recursive sub-columns in Simple infinite classification

Source: Internet
Author: User

  1. <?php
  2.   
  3. /* 
  4. * Find sub-columns
  5.  */  
  6.   
  7. $area=array(
  8. Array(' id'=>1,' name '='China ' ,' parent '=>0),
  9. Array(' id'=>2,' name '='Guangdong ' ,' parent '=>1),
  10. Array(' id '=>3,' name '='shenzhen ', ' Parent ' =>2),
  11. Array(' id'=>4,' name '='Hunan ' ,' parent '=>1),
  12. Array(' id'=>5,' name '='Changsha ' ,' parent '=>4)
  13. );
  14.   
  15. function findson ($arr,$id=0) {
  16. $son=array();
  17. foreach ( $arr as $value) {
  18. if ($value[' parent ']==$id ) {  
  19. $son[]=$value;
  20.         }  
  21.     }  
  22. return $son;
  23. }  
  24.   
  25. /* 
  26. * Find a descendant tree
  27. * @param array passed in by array
  28. * @param int ID passed in
  29.  */  
  30.   
  31. function substree ($arr,$id=0,$level =1) {
  32. static $grandson=array();
  33.       
  34. foreach ( $arr as $value) {
  35. if ($value[' parent ']==$id ) {  
  36. $value[' level ']=$level;
  37. $grandson[]=$value;
  38. Substree ($arr,$value[' id '],$level +1);
  39.         }  
  40.     }  
  41. return $grandson;
  42. }  
  43.   
  44. //var_dump (Findson ($area, 1));   
  45. echo "</br>";
  46. //var_dump (Substree ($area, 2));   
  47.   
  48. /* 
  49. * In order for it to show indentation effect
  50.  */  
  51. $tt=substree ($area, 0, 1);
  52.   
  53. foreach ( $tt as $value) {
  54. echo str_repeat (", $value [" Level "]). $value ["name"]. "</br>"  ;
  55.    }  
  56.      
  57. ?>

Recursive sub-columns in Simple infinite classification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.