Home > Backend Development > PHP Tutorial > 9 lines of code is too wasteful. 5 lines of code is enough. There is no need to recurse to implement tree formatting of infinite classification data.

9 lines of code is too wasteful. 5 lines of code is enough. There is no need to recurse to implement tree formatting of infinite classification data.

WBOY
Release: 2016-07-25 09:06:33
Original
1456 people have browsed it
We know that many open source software’s infinite classifications use recursive algorithms, but we know that recursion wastes time and space (memory).
Last time I also shared my own original infinite classification spanning tree method. An enthusiastic PHP expert netizen gave me valuable suggestions. I tested it and found that the time of this code is very short. Reference: http://www.oschina.net/code/snippet_98719_11296, I sorted it out again and found that after querying the data in the database, we have already set the key value, so in practice, we usually query the formatted main value in the model. The key value corresponds to the form of the data, so we can use such data directly, eliminating a layer of loops. 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(); //格式化好的树
  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 Province'),
  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 City'),
  31. 5 = > array('id' => 5, 'pid' => 2, 'name' => 'Jixi City'),
  32. 6 => array('id' => 6, 'pid' => 4, 'name' => 'Xiangfang District'),
  33. 7 => array('id' => 7, 'pid' => 4, 'name' => 'Nangang District '),
  34. 8 => array('id' => 8, 'pid' => 6, 'name' => 'Hexing Road'),
  35. 9 => array('id' = > 9, 'pid' => 7, 'name' => 'Xidazhi Street'),
  36. 10 => array('id' => 10, 'pid' => 8, ' name' => 'Northeast Forestry University'),
  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' => 'Ganzhou County'),
  41. 15 => array('id' => 15, 'pid' => 13, 'name' => 'Yudu County'),
  42. 16 => array('id' => 16, 'pid' => 14, 'name' => 'Maodian Town'),
  43. 17 => array('id' => 17, 'pid' => 14, ' name' => 'Datian Township'),
  44. 18 => array('id' => 18, 'pid' => 16, 'name' => 'Yiyuan Village'),
  45. 19 = > array('id' => 19, 'pid' => 16, 'name' => 'Shangba Village'),
  46. );
  47. echo "
    ";
  48. print_r(genTree5( $items));
  49. print_r(genTree9($items));
  50. //The latter output format is similar to the former, except that the array key values ​​are different, but it 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 City
  72. [son] => Array
  73. (
  74. [0] => Array
  75. (
  76. [id] => 14
  77. [pid] => 13
  78. [name] => Gan County
  79. [son] => Array
  80. (
  81. [0] => > Maodian Town
  82. [son] => Array
  83. (
  84. [0] => Array
  85. (
  86. [id] => 18
  87. [pid] => 16
  88. [name] => Yiyuan Village
  89. )
  90. [1] => Array
  91. (
  92. [id] => 19
  93. [pid] => [1] => Array
  94. (
  95. [id] => 17
  96. [pid] => 14
  97. [name] => Datian Township
  98. )
  99. )
  100. )
  101. [1] => Array
  102. (
  103. [id] => 15
  104. [pid] => 13
  105. [name] => Yudu County
  106. )
  107. )
  108. )
  109. )
  110. )
  111. [1] => Array
  112. (
  113. [id] => 2
  114. [pid] => 0
  115. [name] => ; Heilongjiang Province
  116. [son] => Array
  117. (
  118. [0] => Array
  119. (
  120. [id] => 4
  121. [pid] => 2
  122. [name] => Harbin City
  123. [ son] => Array
  124. (
  125. [0] => Array
  126. (
  127. [id] => 6
  128. [pid] => 4
  129. [name] => Xiangfang District
  130. [son] => ; Array
  131. (
  132. [0] => Array
  133. (
  134. [id] => 8
  135. [pid] => 6
  136. [name] => Hexing Road
  137. [son] => Array
  138. (
  139. [0] => Array
  140. (
  141. [id] => 10
  142. [pid] => 8
  143. [name] =>
  144. Northeast Forestry University
  145. )
  146. [1] => Array
  147. ( Array
  148. (
  149. [id] => 7
  150. [pid] => 4
  151. [name] => Nangang District
  152. [son] => Array
  153. (
  154. [0] => Array
  155. (
  156. [id ] => 9
  157. [pid] => 7
  158. [name] => Array Array
  159. (
  160. [id] => 5
  161. [pid] => 2
  162. [name] => Jixi City
  163. )
  164. )
  165. )
  166. )*/
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template