PHP Infinite Classification Tree data format code

Source: Internet
Author: User
We know that many open source software's infinite classification is the recursive algorithm, but we know that recursion is a waste of time, but also a waste of space (memory),
Last time I also shared a my own original infinite classification spanning tree method, a enthusiastic php expert netizens gave me a valuable advice, I tested it, this code of time very short reference: http://www.oschina.net/code/snippet_98719_ 11296, I once again, found that the database query data, we have set the key value, so in practice, we generally in the model query out the format into the primary key value corresponding data form, so we can directly use such data, there is less a layer of cycle. 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;} $items 1 = array (' id ' = = 1, ' pid ' = = 0, ' name ' = ' one '), array (' id ' = = ', ' pid ' = = 0, ' name ' ' + ' (' = '), array (' id ' = = 2, ' pid ' = + 1, ' name ' = ' two ') ', array (' id ' = ' = ', ' pid ' = ' = ', ' NA Me ' + ' two '), array (' id ' = = 3, ' pid ' = + 1, ' name ' = ' two '), array (' id ' = +, ' pid ' = 11, ' Name ' = = ' two '), array (' id ' = = 9, ' pid ' = 1, ' name ' = ' two '), array (' id ' = +, ' pid ' = + 1, ' Name ' = ' two '), array (' id ' = = 4, ' pid ' = 9, ' name ' = ' three '), array (' ID ') = 6, ' pid ' = + 9, ' name ' = ' three '), array (' id ' = = 7, ' pid ' = 4, ' name ' = ' four '), array (' ID ')  = 8, ' pid ' = 4, ' name ' = ' four '), array (' id ' = = 5, ' pid ' = 4, ' name ' = ' four '), array (' ID ') = +, ' pid ' = 4, ' name ' + ' four ', array (' id ' = +, ' pid ' = 8, ' name ' = ' five ') '), Array (' I d ' = +, ' pid ' = 8, ' name ' = ' five-level ', ' array ' (' id ' = = ', ' pid ' = 8, ' name ' = ' five ') ', Array ( ' id ' = +, ' pid ' = +, ' name ' = ' Six level 64 '); Var_dump (Gentree ($items 1));

Here are the additions:

/** * 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 data into a tree structure * @author xuefen.tong * @param array $items * @return array */function genTree9 ($items) {$tree = array (); Well-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 (' id ' = = 1, ' pid ' = 0, ' name ' = ' Jiangxi '), 2 = = Array (' id ' = = 2, ' pid ' = 0, ' name ' = ' Heilongjiang Province '), 3 = = Array (' id ' = 3, ' pid ' = = 1, ' name ' = ' Nanchang '), 4 = = Array (' id ' = = 4, ' pi d ' + 2, ' name ' = ' Harbin City '), 5 = = Array (' id ' = 5, ' pid ' = = 2, ' name ' = ' Jixi '), 6 = = Array (' ID ' =&G T 6, ' pid ' = 4, ' name ' = = ' Xiangfang District '), 7 = = Array (' id ' = = 7, ' pid ' = 4, ' name ' = ' Nangang '), 8 = = Array (' ID  ' = = 8, ' pid ' = 6, ' name ' = ' and Hing Road '), 9 = = Array (' id ' = = 9, ' pid ' = 7 ', ' name ' = ' West big straight Street '), ten = Array (' id ' = +, ' pid ' = + 8, ' name ' = ' Northeast Forestry University '),  An array (' id ' = ' + ', ' pid ' = 9, ' name ' = ' Harbin Institute of Technology '), and array (' id ' = ' + ', ' pid ' = 8, ' Nam E ' + ' harbin Normal University '), the array (' id ' = ' = ', ' pid ' = 1, ' name ' = ' Ganzhou '), + = array (' id ' = + ', ' pi d ' = +, ' name ' = ' Gan County '), and array (' id ' = +, ' pid ' = ' + '), ' name ' = ' Yudu County '), + = array (' ID ') = +, ' pid ' = +, ' name ' and ' Mao Shop Town ', ' + ' + ' array ' (' id ' = ' + ', ' pid ' = ' = ', ' name ' = ' Ota township '), =&G T Array (' id ' = +, ' pid ' = + ', ' name ' = ' righteousness village '), ' + ' array (' id ' = ' + ', ' pid ' = ' + '), ' name ' = ' ' Shang Ba Village ' '),); Print_r (GenTree5 ($items));p Rint_r (GenTree9 ($items));//The latter output format, the former similar, but the array key value is not the same, but does not affect the data structure/*array ([0] = = Array ([ID ] = 1 [PID] + 0 [name] = = Jiangxi [son] = = Array ([0] = = Array ([i            D] + 3 [pid] = 1 [name] = Nanchang) [1] = = Array (     [id] = 13       [PID] = 1 [name] = = Ganzhou [son] = = Array ([0] = Arra                     Y ([id] = [PID] [name] = + Gan County                           [Son] = array ([0] = = Array (                          [ID] [PID] = [name] + Mao Dian Zhen                              [Son] = array ([0] = = Array                              ([id] + [pid] = 16                              [Name] and righteousness source village) [1] = = Array                              ([id] + [pid] = 16 [Name] =&GT Shangba village)) [1] =& Gt                          Array ([id] + [pid] = 14 [Name] = Ota township)) [1] = = Array ([id] = [pid] = [Name] ] (Yudou County))) [1] = = Array ([id] = 2 [PID] =& Gt 0 [Name] = Heilongjiang province [son]-= Array ([0] = = Array ([id] = 4 [P                ID] + 2 [name] + harbin [son] = = Array ([0] = = Array ([id] = 6 [PID] + 4 [name] = = Xiangfang District [ Son] = ArRay ([0] = = Array ([id] = 8                          [PID] = 6 [name] = and Xing Lu [son] = Array                              ([0] = = Array (                               [ID] [PID] + 8 [name] = =                              Northeast Forestry University) [1] = = Array ( [ID] [PID] = 8 [name]                        (Harbin Normal University)) ))) [1] = = Array ([id] =&gt ; 7 [PiD] = 4 [name] and Nangang District [son] = = Array ([0                      ] = = Array ([id] = 9 [PID] + 7 [Name] = West Big Straight Street [son] = = Array ([0] = Ar                          Ray ([ID] + [pid] = 9                        [Name] = Harbin Institute of Technology)          )))) [1] = = Array ([id] = 5 [PID] = 2 [name] + Jixi))) */

The

is extremely simple and effective!!! Very good!

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.