Nine lines of code is too wasteful, and five lines of code are sufficient. you do not need to recursively format the tree of infinitely classified data.

Source: Internet
Author: User
Nine lines of code is too wasteful, and five lines of code are sufficient. you do not need to recursively format the tree of infinitely classified data.
We know that the infinite classification of many open-source software uses recursive algorithms, but we know that recursion is a waste of time and space (memory ),
Last time I also shared my original method of generating tree with unlimited categories. a enthusiastic php expert gave me valuable suggestions. I tested it, this code takes a very short time. for details, refer. The code is also very concise.

  1. /**
  2. * This method is provided by @ Tonton
  3. * Http://my.oschina.net/u/918697
  4. * @ Date 2012-12-12
  5. */
  6. Function genTree5 ($ items ){
  7. Foreach ($ items as $ item)
  8. $ Items [$ item ['pid '] ['son'] [$ item ['id'] = & $ items [$ item ['id'];
  9. Return isset ($ items [0] ['son'])? $ Items [0] ['son']: array ();
  10. }
  11. /**
  12. * Format the data into a tree structure.
  13. * @ Author Xuefen. Tong
  14. * @ Param array $ items
  15. * @ Return array
  16. */
  17. Function genTree9 ($ items ){
  18. $ Tree = array (); // formatted tree
  19. Foreach ($ items as $ item)
  20. If (isset ($ items [$ item ['pid '])
  21. $ Items [$ item ['pid '] ['son'] [] = & $ items [$ item ['id'];
  22. Else
  23. $ Tree [] = & $ items [$ item ['id'];
  24. Return $ tree;
  25. }
  26. $ Items = array (
  27. 1 => array ('id' => 1, 'pid '=> 0, 'name' => 'jiangxi '),
  28. 2 => array ('id' => 2, 'pid '=> 0, 'name' => 'heilongjiang province '),
  29. 3 => array ('id' => 3, 'pid '=> 1, 'name' => 'nanchang city '),
  30. 4 => array ('id' => 4, 'pid '=> 2, 'name' => 'Harbin '),
  31. 5 => array ('id' => 5, 'pid '=> 2, 'name' => 'jixi city '),
  32. 6 => array ('id' => 6, 'pid '=> 4, 'name' => 'xiangfang region '),
  33. 7 => array ('id' => 7, 'pid '=> 4, 'name' => 'nangang '),
  34. 8 => array ('id' => 8, 'pid '=> 6, 'name' =>' and Xinglu '),
  35. 9 => array ('id' => 9, 'pid '=> 7, 'name' => 'westbrook street '),
  36. 10 => array ('id' => 10, 'pid '=> 8, 'name' => 'Northeast forestry Emy '),
  37. 11 => array ('id' => 11, 'pid '=> 9, 'name' => 'Harbin Institute of Technology '),
  38. 12 => array ('id' => 12, 'pid '=> 8, 'name' => 'Harbin Normal University '),
  39. 13 => array ('id' => 13, 'pid '=> 1, 'name' => 'ganzhou city '),
  40. 14 => array ('id' => 14, 'pid '=> 13, 'name' => 'ganxian '),
  41. 15 => array ('id' => 15, 'pid '=> 13, 'name' => 'yudu County '),
  42. 16 => array ('id' => 16, 'pid '=> 14, 'name' => 'moudian Zhen '),
  43. 17 => array ('id' => 17, 'pid' => 14, 'name' => 'tagname '),
  44. 18 => array ('id' => 18, 'pid '=> 16, 'name' => 'yiyuan cune '),
  45. 19 => array ('id' => 19, 'pid '=> 16, 'name' => 'shangba cune '),
  46. );
  47. Echo"
    ";
  48. Print_r (genTree5 ($ items ));
  49. Print_r (genTree9 ($ items ));
  50. // The latter output format. the former format is similar, but the array key values are different, but the data structure is not affected.
  51. /*
  52. Array
  53. (
  54. [0] => Array
  55. (
  56. [Id] => 1
  57. [Pid] => 0
  58. [Name] => Jiangxi
  59. [Son] => Array
  60. (
  61. [0] => Array
  62. (
  63. [Id] => 3
  64. [Pid] => 1
  65. [Name] => Nanchang City
  66. )
  67. [1] => Array
  68. (
  69. [Id] => 13
  70. [Pid] => 1
  71. [Name] => Ganzhou City
  72. [Son] => Array
  73. (
  74. [0] => Array
  75. (
  76. [Id] => 14
  77. [Pid] => 13
  78. [Name] => Ganxian County
  79. [Son] => Array
  80. (
  81. [0] => Array
  82. (
  83. [Id] => 16
  84. [Pid] => 14
  85. [Name] => moudian town
  86. [Son] => Array
  87. (
  88. [0] => Array
  89. (
  90. [Id] => 18
  91. [Pid] => 16
  92. [Name] => Yiyuan village
  93. )
  94. [1] => Array
  95. (
  96. [Id] => 19
  97. [Pid] => 16
  98. [Name] => Shangba village
  99. )
  100. )
  101. )
  102. [1] => Array
  103. (
  104. [Id] => 17
  105. [Pid] => 14
  106. [Name] => Datian Township
  107. )
  108. )
  109. )
  110. [1] => Array
  111. (
  112. [Id] => 15
  113. [Pid] => 13
  114. [Name] => Yudu County
  115. )
  116. )
  117. )
  118. )
  119. )
  120. [1] => Array
  121. (
  122. [Id] => 2
  123. [Pid] => 0
  124. [Name] => Heilongjiang province
  125. [Son] => Array
  126. (
  127. [0] => Array
  128. (
  129. [Id] => 4
  130. [Pid] => 2
  131. [Name] => Harbin
  132. [Son] => Array
  133. (
  134. [0] => Array
  135. (
  136. [Id] => 6
  137. [Pid] => 4
  138. [Name] => Xiangfang District
  139. [Son] => Array
  140. (
  141. [0] => Array
  142. (
  143. [Id] => 8
  144. [Pid] => 6
  145. [Name] => hexinglu
  146. [Son] => Array
  147. (
  148. [0] => Array
  149. (
  150. [Id] => 10
  151. [Pid] => 8
  152. [Name] =>
  153. Northeast Forestry University
  154. )
  155. [1] => Array
  156. (
  157. [Id] => 12
  158. [Pid] => 8
  159. [Name] =>
  160. Harbin Normal University
  161. )
  162. )
  163. )
  164. )
  165. )
  166. [1] => Array
  167. (
  168. [Id] => 7
  169. [Pid] => 4
  170. [Name] => Nangang District
  171. [Son] => Array
  172. (
  173. [0] => Array
  174. (
  175. [Id] => 9
  176. [Pid] => 7
  177. [Name] => westbound street
  178. [Son] => Array
  179. (
  180. [0] => Array
  181. (
  182. [Id] => 11
  183. [Pid] => 9
  184. [Name] =>
  185. Harbin Institute of Technology
  186. )
  187. )
  188. )
  189. )
  190. )
  191. )
  192. )
  193. [1] => Array
  194. (
  195. [Id] => 5
  196. [Pid] => 2
  197. [Name] => Jixi city
  198. )
  199. )
  200. )
  201. )*/

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.