Rumah >pembangunan bahagian belakang >tutorial php >9行代码太浪费了,5行代码足矣,不用递归实现无限分类数据的树形格式化

9行代码太浪费了,5行代码足矣,不用递归实现无限分类数据的树形格式化

WBOY
WBOYasal
2016-07-25 09:06:331458semak imbas
我们知道很多开源软件的无限分类都是采用递归的算法,但是我们知道递归即浪费时间,又浪费空间(内存),
上次我也分享了个我自己原创的无限分类生成树的方法,一热心php高手网友给我提出了宝贵的建议,我测试了一下,这段代码的时间非常之短,参考: http://www.oschina.net/code/snippet_98719_11296,我再次整理了一下,发现数据库查询出数据之后,我们就已经定好了键值,因而实践中,我们一般会在model中查询出格式化成主键值对应数据的形式,因而我们可以直接用这样的数据,就少了一层循环。代码也非常简洁。
  1. /**
  2. * 此方法由@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. * 将数据格式化成树形结构
  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' => '江西省'),
  28. 2 => array('id' => 2, 'pid' => 0, 'name' => '黑龙江省'),
  29. 3 => array('id' => 3, 'pid' => 1, 'name' => '南昌市'),
  30. 4 => array('id' => 4, 'pid' => 2, 'name' => '哈尔滨市'),
  31. 5 => array('id' => 5, 'pid' => 2, 'name' => '鸡西市'),
  32. 6 => array('id' => 6, 'pid' => 4, 'name' => '香坊区'),
  33. 7 => array('id' => 7, 'pid' => 4, 'name' => '南岗区'),
  34. 8 => array('id' => 8, 'pid' => 6, 'name' => '和兴路'),
  35. 9 => array('id' => 9, 'pid' => 7, 'name' => '西大直街'),
  36. 10 => array('id' => 10, 'pid' => 8, 'name' => '东北林业大学'),
  37. 11 => array('id' => 11, 'pid' => 9, 'name' => '哈尔滨工业大学'),
  38. 12 => array('id' => 12, 'pid' => 8, 'name' => '哈尔滨师范大学'),
  39. 13 => array('id' => 13, 'pid' => 1, 'name' => '赣州市'),
  40. 14 => array('id' => 14, 'pid' => 13, 'name' => '赣县'),
  41. 15 => array('id' => 15, 'pid' => 13, 'name' => '于都县'),
  42. 16 => array('id' => 16, 'pid' => 14, 'name' => '茅店镇'),
  43. 17 => array('id' => 17, 'pid' => 14, 'name' => '大田乡'),
  44. 18 => array('id' => 18, 'pid' => 16, 'name' => '义源村'),
  45. 19 => array('id' => 19, 'pid' => 16, 'name' => '上坝村'),
  46. );
  47. echo "
    ";
  48. print_r(genTree5($items));
  49. print_r(genTree9($items));
  50. //后者输出格式,前者类似,只是数组键值不一样,不过不影响数据结构
  51. /*
  52. Array
  53. (
  54. [0] => Array
  55. (
  56. [id] => 1
  57. [pid] => 0
  58. [name] => 江西省
  59. [son] => Array
  60. (
  61. [0] => Array
  62. (
  63. [id] => 3
  64. [pid] => 1
  65. [name] => 南昌市
  66. )
  67. [1] => Array
  68. (
  69. [id] => 13
  70. [pid] => 1
  71. [name] => 赣州市
  72. [son] => Array
  73. (
  74. [0] => Array
  75. (
  76. [id] => 14
  77. [pid] => 13
  78. [name] => 赣县
  79. [son] => Array
  80. (
  81. [0] => Array
  82. (
  83. [id] => 16
  84. [pid] => 14
  85. [name] => 茅店镇
  86. [son] => Array
  87. (
  88. [0] => Array
  89. (
  90. [id] => 18
  91. [pid] => 16
  92. [name] => 义源村
  93. )
  94. [1] => Array
  95. (
  96. [id] => 19
  97. [pid] => 16
  98. [name] => 上坝村
  99. )
  100. )
  101. )
  102. [1] => Array
  103. (
  104. [id] => 17
  105. [pid] => 14
  106. [name] => 大田乡
  107. )
  108. )
  109. )
  110. [1] => Array
  111. (
  112. [id] => 15
  113. [pid] => 13
  114. [name] => 于都县
  115. )
  116. )
  117. )
  118. )
  119. )
  120. [1] => Array
  121. (
  122. [id] => 2
  123. [pid] => 0
  124. [name] => 黑龙江省
  125. [son] => Array
  126. (
  127. [0] => Array
  128. (
  129. [id] => 4
  130. [pid] => 2
  131. [name] => 哈尔滨市
  132. [son] => Array
  133. (
  134. [0] => Array
  135. (
  136. [id] => 6
  137. [pid] => 4
  138. [name] => 香坊区
  139. [son] => Array
  140. (
  141. [0] => Array
  142. (
  143. [id] => 8
  144. [pid] => 6
  145. [name] => 和兴路
  146. [son] => Array
  147. (
  148. [0] => Array
  149. (
  150. [id] => 10
  151. [pid] => 8
  152. [name] =>
  153. 东北林业大学
  154. )
  155. [1] => Array
  156. (
  157. [id] => 12
  158. [pid] => 8
  159. [name] =>
  160. 哈尔滨师范大学
  161. )
  162. )
  163. )
  164. )
  165. )
  166. [1] => Array
  167. (
  168. [id] => 7
  169. [pid] => 4
  170. [name] => 南岗区
  171. [son] => Array
  172. (
  173. [0] => Array
  174. (
  175. [id] => 9
  176. [pid] => 7
  177. [name] => 西大直街
  178. [son] => Array
  179. (
  180. [0] => Array
  181. (
  182. [id] => 11
  183. [pid] => 9
  184. [name] =>
  185. 哈尔滨工业大学
  186. )
  187. )
  188. )
  189. )
  190. )
  191. )
  192. )
  193. [1] => Array
  194. (
  195. [id] => 5
  196. [pid] => 2
  197. [name] => 鸡西市
  198. )
  199. )
  200. )
  201. )*/
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn