This article mainly introduces the php infinite classification tree data formatting code. For more information, see. we know that many open-source software's infinite classification 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!
For more articles about the php unlimited classification tree data formatting code, please follow the PHP Chinese network!