In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "php infinite classification tree data formatting code sharing", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "php infinite classification tree data formatting code sharing"!
We know that many infinite classifications of open source software use recursive algorithms, but we know that recursion wastes both time and space (memory).
Last time I also shared my own original infinite classification spanning tree method, an enthusiastic php master netizen gave me valuable advice, I tested, 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 the database queried the data, we had set the key value, so in practice We usually query in model to format the data corresponding to the primary key value, so we can use such data directly, without a layer of loops. The code is also very concise.
The original code:
Function genTree ($items,$id='id',$pid='pid',$son = 'children') {$tree = array (); / / formatted tree $tmpMap = array (); / / temporary flat data foreach ($items as $item) {$tmpMap [$item [$id]] = $item;} foreach ($items as $item) {if (isset ($tmpMap [$item [$pid])) {$tmpMap [$item [$pid]] [$son] [] = & $tmpMap [$item [$id]] } else {$tree [] = & $tmpMap [$item [$id]];}} unset ($tmpMap); return $tree } $items1 = array (array ('id' = > 1,' pid' = > 0, 'name' = >' level 11'), array ('id' = > 11,' pid' = > 0, 'name' = >' level 12'), array ('id' = > 2,' pid' = > 1, 'name' = >' level 21'), array ('id' = > 10,' pid' = > 11, 'name' = >' level 22') Array ('id' = > 3,' pid' = > 1, 'name' = >' level 23'), array ('id' = > 12,' pid' = > 11, 'name' = >' level 24'), array ('id' = > 9,' pid' = > 1, 'name' = >' level 25'), array ('id' = > 14,' pid' = > 1, 'name' = >' level 26'), array ('id' = > 4) 'pid' = > 9,' name' = > 'level 31'), array ('id' = > 6,' pid' = > 9, 'name' = >' level 32'), array ('id' = > 7,' pid' = > 4, 'name' = >' level 41'), array ('id' = > 8,' pid' = > 4, 'name' = >' level 42'), array ('id' = > 5,' pid' = > 4') ), array ('id' = > 13,' pid' = > 4, 'name' = >' level 44'), array ('id' = > 15,' pid' = > 8, 'name' = >' level 51'), array ('id' = > 16,' pid' = > 8, 'name' = >' level 52'), array ('id' = > 17,' pid' = > 8') 'name' = >' level 5 53'), array ('id' = > 18,' pid' = > 16, 'name' = >' level 64')) Var_dump (genTree ($items1))
The following is a supplement:
/ * * this method is provided by @ Tonton * http://my.oschina.net/u/918697 * @ date 2012-12-12 * / function genTree5 ($items) {foreach ($items as $item) $items [$item ['pid']] [' son'] [$item ['id']] = & $items [$item [' id']; return isset ($items [0] ['son'])? $items [0] [' son']: array () } / * format the data into a tree structure * @ author Xuefen.Tong * @ param array $items * @ return array * / function genTree9 ($items) {$tree = array (); / / formatted tree foreach ($items as $item) if (isset ($items [$item ['pid'])) $items [$item [' pid']] ['son'] [] = & $items [$item [' id']] Else $tree [] = & $items [$item ['id']]; return $tree } $items = array (1 = > array ('id' = > 1,' pid' = > 0, 'name' = >' Jiangxi Province'), 2 = > array ('id' = > 2,' pid' = > 0, 'name' = >' Heilongjiang Province'), 3 = > array ('id' = > 3,' pid' = > 1, 'name' = > Nanchang'), 4 = > array ('id' = > 4,' pid' = > 2, 'name' = > Harbin') 5 = > array ('id' = > 5,' pid' = > 2, 'name' = >' Jixi City'), 6 = > array ('id' = > 6,' pid' = > 4, 'name' = >' Xiangfang District'), 7 = > array ('id' = > 7,' pid' = > 4, 'name' = >' Nangang District'), 8 = > array ('id' = > 8,' pid' = > 6, 'name' = >' Hexing Road'), 9 = > array ('id' = > 9') 'pid' = > 7,' name' = > 'West Dazhi Street'), 10 = > array ('id' = > 10,' pid' = > 8, 'name' = >' Northeast Forestry University'), 11 = > array ('id' = > 11,' pid' = > 9, 'name' = >' Harbin Institute of Technology'), 12 = > array ('id' = > 12,' pid' = > 8, 'name' = > Harbin normal University'), 13 = > array ('id' = > 13) 'pid' = > 1,' name' = > 'Ganzhou'), 14 = > array ('id' = > 14,' pid' = > 13, 'name' = >' Ganxian'), 15 = > array ('id' = > 15,' pid' = > 13, 'name' = >' Yudu County'), 16 = > array ('id' = > 16,' pid' = > 14, 'name' = >' Maodian Town'), 17 = > array ('id' = > 17,' pid' = > 14') 'name' = >' Datian Township'), 18 = > array ('id' = > 18,' pid' = > 16, 'name' = >' Yiyuan Village'), 19 = > array ('id' = > 19,' pid' = > 16, 'name' = > Shangba Village') Print_r (genTree5 ($items)); print_r (genTree9 ($items)) / / the output format of the latter is similar, except that the array key values are not the same. However, it does not affect the data structure / * Array ([0] = > Array ([id] = > 1 [pid] = > 0 [name] = > Jiangxi Province [son] = > Array ([0] = > Array ([id] = > 3 [pid] = > 1 [name] = > Nanchang City) [1] = > Array) ([id] = > 13 [pid] = > 1 [name] = > Ganzhou [son] = > Array ([0] = > Array ([id] = > 14 [pid] = > 13) [name] = > Ganxian [son] = > Array ([0] = > Array ([id] = > 16 [pid] = > 14 [name] = > Maodian Town [son] = > Array ([0] = > Array ([id] = > 18 [pid] = > 16) [name] = > Yiyuan Village) [1] = > Array ([id] = > 19 [pid] = > 16 [name] )) [1] = > Array ([id] = > 17 [pid] = > 14) [name] = > Datian Township)) [1] = > Array ([id] = > 15 [pid] = > 13 [name] ] = > Yudu County) [1] = > Array ([id] = > 2 [pid] = > 0 [name] = > Heilongjiang Province [son] = > Array ([0] = > Array ([id] = > 4 [pid] = > 2) [name] = > Harbin [son] = > Array ([0] = > Array ([id] = > 6 [pid] = > 4 [name] = > Xiangfang District [son] = > Array ([0] = > Array ([id] = > 8 [pid] = > 6 [name] = > Hexing Road [son] = > Array ( [0] = > Array ([id] = > 10 [pid] = > 8 [name] = > Northeast Forestry University ) [1] = > Array ([id] = > 12 [pid] = > 8 [name] = > Harbin normal University )) [1] = > Array ([id] = > 7 [pid] = > 4 [name] = > Nangang District [son] = > Array ([0] = > Array ([id] = > 9 [pid] = > 7 [name] = > Xidazhi Street [son] = > Array ([0] = > Array ([id] = > 11 [pid] = > 9 [name] = > (Harbin Institute of Technology) [1] = > Array ([id] = > 5 [pid] = > 2 [name] = > Jixi City) * / here I believe you have a deeper understanding of "php infinite classification tree data formatting code sharing". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.