9 lines of code is too wasted, 5 lines of code is enough, no recursive implementation of infinite categorical data in the form of a tree

Source: Internet
Author: User
We know that many open source software's infinite classification is the recursive algorithm, but we know that recursion is a waste of time, but also a waste of space (memory),
Last time I also shared a my own original infinite classification spanning tree method, a enthusiastic php expert netizens gave me a valuable advice, I tested it, this code of time is very short, reference: http://www.oschina.net/code/snippet_98719_ 11296, I once again, found that the database query data, we have set the key value, so in practice, we generally in the model query out the format into the primary key value corresponding data form, so we can directly use such data, there is less a layer of cycle. 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 data into a tree structure
  13. * @author Xuefen.tong
  14. * @param array $items
  15. * @return Array
  16. */
  17. function GenTree9 ($items) {
  18. $tree = Array (); Well-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 '),
  30. 4 = = Array (' id ' = = 4, ' pid ' = 2, ' name ' = ' Harbin City '),
  31. 5 = = Array (' id ' = = 5, ' pid ' = 2, ' name ' = ' Jixi '),
  32. 6 = = Array (' id ' = = 6, ' pid ' = 4, ' name ' = ' Xiangfang District '),
  33. 7 = = Array (' id ' = = 7, ' pid ' = + 4, ' name ' = ' Nangang '),
  34. 8 = = Array (' id ' = = 8, ' pid ' = 6, ' name ' = ' and Hing Road '),
  35. 9 = = Array (' id ' = = 9, ' pid ' = ' 7 ', ' name ' = ' West Dazhi street '),
  36. The array (' id ' = +, ' pid ' = 8, ' name ' = ' Northeast Forestry University '),
  37. One-by-one and array (' id ' = = ', ' pid ' = + 9, ' name ' = ' Harbin Institute of Technology '),
  38. The array (' id ' = +, ' pid ' = + 8, ' name ' = ' harbin Normal University '),
  39. The array (' id ' = ' + ', ' pid ' = + 1, ' name ' = ' Ganzhou '),
  40. The array (' id ' = ' + ', ' pid ' = ' = ', ' name ' = ' ' Gan County '),
  41. The array (' id ' = +, ' pid ' = ' + ', ' name ' = ' ' Yudu County '),
  42. A + = array (' id ' = ' + ', ' pid ' = ' + ', ' name ' = ' Mao Shop town '),
  43. + = array (' id ' = +, ' pid ' = ' = ', ' name ' = ' Ota township '),
  44. The array (' id ' = ' + ', ' pid ' = ' + ', ' name ' = ' yi Yuan cun '),
  45. The array (' id ' = +, ' pid ' = ' + ', ' name ' = ' ' Shang Ba Village '),
  46. );
  47. echo "
    ";
  48. Print_r (GenTree5 ($items));
  49. Print_r (GenTree9 ($items));
  50. The latter output format, the former similar, but the array key value is not the same, but does not affect the data structure
  51. /*
  52. Array
  53. (
  54. [0] = = Array
  55. (
  56. [id] = 1
  57. [PID] = 0
  58. [Name] = Jiangxi Province
  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
  72. [Son] = Array
  73. (
  74. [0] = = Array
  75. (
  76. [id] = 14
  77. [PID] = 13
  78. [Name] = Gan County
  79. [Son] = Array
  80. (
  81. [0] = = Array
  82. (
  83. [id] = 16
  84. [PID] = 14
  85. [Name] + Mao Dian Zhen
  86. [Son] = Array
  87. (
  88. [0] = = Array
  89. (
  90. [id] = 18
  91. [PID] = 16
  92. [Name] and righteousness Source 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] = Ota 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 City
  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]/= Hing Road
  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] = West Tai Straight 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
  198. )
  199. )
  200. )
  201. )*/
Copy Code
  • Related Article

    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.