当前位置:Gxlcms > PHP教程 > 9行代码太浪费了,5行代码足矣,不用递归实现无限分类数据的树形格式化

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

时间:2021-07-01 10:21:17 帮助过:22人阅读

我们知道很多开源软件的无限分类都是采用递归的算法,但是我们知道递归即浪费时间,又浪费空间(内存),
上次我也分享了个我自己原创的无限分类生成树的方法,一热心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. /*
  53. Array
  54. (
  55. [0] => Array
  56. (
  57. [id] => 1
  58. [pid] => 0
  59. [name] => 江西省
  60. [son] => Array
  61. (
  62. [0] => Array
  63. (
  64. [id] => 3
  65. [pid] => 1
  66. [name] => 南昌市
  67. )
  68. [1] => Array
  69. (
  70. [id] => 13
  71. [pid] => 1
  72. [name] => 赣州市
  73. [son] => Array
  74. (
  75. [0] => Array
  76. (
  77. [id] => 14
  78. [pid] => 13
  79. [name] => 赣县
  80. [son] => Array
  81. (
  82. [0] => Array
  83. (
  84. [id] => 16
  85. [pid] => 14
  86. [name] => 茅店镇
  87. [son] => Array
  88. (
  89. [0] => Array
  90. (
  91. [id] => 18
  92. [pid] => 16
  93. [name] => 义源村
  94. )
  95. [1] => Array
  96. (
  97. [id] => 19
  98. [pid] => 16
  99. [name] => 上坝村
  100. )
  101. )
  102. )
  103. [1] => Array
  104. (
  105. [id] => 17
  106. [pid] => 14
  107. [name] => 大田乡
  108. )
  109. )
  110. )
  111. [1] => Array
  112. (
  113. [id] => 15
  114. [pid] => 13
  115. [name] => 于都县
  116. )
  117. )
  118. )
  119. )
  120. )
  121. [1] => Array
  122. (
  123. [id] => 2
  124. [pid] => 0
  125. [name] => 黑龙江省
  126. [son] => Array
  127. (
  128. [0] => Array
  129. (
  130. [id] => 4
  131. [pid] => 2
  132. [name] => 哈尔滨市
  133. [son] => Array
  134. (
  135. [0] => Array
  136. (
  137. [id] => 6
  138. [pid] => 4
  139. [name] => 香坊区
  140. [son] => Array
  141. (
  142. [0] => Array
  143. (
  144. [id] => 8
  145. [pid] => 6
  146. [name] => 和兴路
  147. [son] => Array
  148. (
  149. [0] => Array
  150. (
  151. [id] => 10
  152. [pid] => 8
  153. [name] =>
  154. 东北林业大学
  155. )
  156. [1] => Array
  157. (
  158. [id] => 12
  159. [pid] => 8
  160. [name] =>
  161. 哈尔滨师范大学
  162. )
  163. )
  164. )
  165. )
  166. )
  167. [1] => Array
  168. (
  169. [id] => 7
  170. [pid] => 4
  171. [name] => 南岗区
  172. [son] => Array
  173. (
  174. [0] => Array
  175. (
  176. [id] => 9
  177. [pid] => 7
  178. [name] => 西大直街
  179. [son] => Array
  180. (
  181. [0] => Array
  182. (
  183. [id] => 11
  184. [pid] => 9
  185. [name] =>
  186. 哈尔滨工业大学
  187. )
  188. )
  189. )
  190. )
  191. )
  192. )
  193. )
  194. [1] => Array
  195. (
  196. [id] => 5
  197. [pid] => 2
  198. [name] => 鸡西市
  199. )
  200. )
  201. )
  202. )*/

人气教程排行