Php unlimited classification tree data formatting code, php tree

Source: Internet
Author: User

Php unlimited classification tree data formatting code, php tree

We know that the infinite classification of many open-source software uses recursive algorithms, but we know that recursion is a waste of time and space (memory ),
Last time I also shared my original method of generating tree with unlimited categories. A enthusiastic php expert gave me valuable suggestions. I tested it, the time for this code is very short. For more information, refer to explain. The Code is also very concise.

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 ('id' => 1, 'pid '=> 0, 'name' => 'level 1 11 '), array ('id' => 11, 'pid '=> 0, 'name' => 'level 1 12'), array ('id' => 2, 'pid '=> 1, 'name' => 'level 2 21'), array ('id' => 10, 'pid' => 11, 'name' => 'level 2 22'), array ('id' => 3, 'pid '=> 1, 'name' => 'level 2 23 '), array ('id' => 12, 'pid '=> 11, 'name' => 'second-level 24'), array ('id' => 9, 'pid '=> 1, 'name' => 'level 2 25'), array ('id' => 14, 'pid' => 1, 'name' => 'level 2 26'), array ('id' => 4, 'pid '=> 9, 'name' => 'level 3 31 '), array ('id' => 6, 'pid '=> 9, 'name' => 'third-level 32'), array ('id' => 7, 'pid '=> 4, 'name' => 'l4 41'), array ('id' => 8, 'pid '=> 4, 'name' => 'l4 42 '), array ('id' => 5, 'pid' => 4, 'name' => 'l4 43 '), array ('id' => 13, 'pid '=> 4, 'name' => 'l4 44'), array ('id' => 15, 'pid '=> 8, 'name' => 'level 5 51'), array ('id' => 16, 'pid' => 8, 'name' => 'level 5 52'), array ('id' => 17, 'pid '=> 8, 'name' => 'level 5 53 '), array ('id' => 18, 'pid '=> 16, 'name' => '64 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 '), 2 => array ('id' => 2, 'pid '=> 0, 'name' => 'heilongjiang province '), 3 => array ('id' => 3, 'pid '=> 1, 'name' => 'nanchang City '), 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 Region '), 7 => array ('id' => 7, 'pid '=> 4, 'name' => 'nangang '), 8 => array ('id' => 8, 'pid '=> 6, 'name' =>' and xinglu '), 9 => array ('id' => 9, 'pid '=> 7, 'name' => 'westbrook Street '), 10 => array ('id' => 10, 'pid '=> 8, 'name' => 'northeast forestry Emy '), 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 City '), 14 => array ('id' => 14, 'pid '=> 13, 'name' => 'ganxian '), 15 => array ('id' => 15, 'pid '=> 13, 'name' => 'yudu County '), 16 => array ('id' => 16, 'pid '=> 14, 'name' => 'moudian Zhen '), 17 => array ('id' => 17, 'pid' => 14, 'name' => 'tagname '), 18 => array ('id' => 18, 'pid '=> 16, 'name' => 'yiyuan cune '), 19 => array ('id' => 19, 'pid '=> 16, 'name' => 'shangba cune '),); print_r (genTree5 ($ items); print_r (genTree9 ($ items); // The latter output format. The former format is similar, but the array key values are different, however, the data structure is not affected./* Array ([0] => Array ([id] => 1 [pid] => 0 [name] => Jiangxi [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] => moudian town [son] => Array ([0] => Array ([id] => 18 [pid] => 16 [name] => yiyuan Village) [1] => Array ([id] => 19 [pid] => 16 [name] => Shangba Village ))) [1] => Array ([id] => 17 [pid] => 14 [name] => Datian ))) [1] => Array ([id] => 15 [pid] => 13 [name] => yudu County ))))) [1] => Array ([id] => 2 [pid] => 0 [name] => Heilongjiang [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] => westbound Street [son] => Array ([0] => Array ([id] => 11 [pid] => 9 [name] => Harbin Institute of Technology ))))))) [1] => Array ([id] => 5 [pid] => 2 [name] => Jixi City ))))*/

Extremely simple and effective !!! Very useful!

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.