PHP Infinite Classification Tree Data format Code Examples of detailed

Source: Internet
Author: User
This article mainly introduces the PHP infinite classification of tree data format code, the need for friends can refer to the following

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 ' =&Gt 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 ' = The ' pid ' = 4, ' name ' = ' four ', array (' id ' = +, ' pid ' = 8, ' name ' = ' five ') ', array (' ID ' =&gt ; +, ' pid ' = 8, ' name ' + ' five-level '), array (' id ' = ' = ', ' pid ' = 8, ' name ' = ' five '), array (' ID ' =&gt ; , ' 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, ' name ' = = ' harbin Normal University '), and the array (' id ' = ' + ', ' pid ' = 1, ' name ' = ' Ganzhou '), + = array (' id ' = + ' pid ' = + ', ' name ' = ' Gan County '), and the array (' id ' = +, ' pid ' = ' + '), ' name ' = ' Yudu County '), + = array (' id ' = >, ' pid ' = +, ' name ' and ' Mao Dian Zhen ', ' the ' id ' = ' + ', ' pid ' = ' + ', ' name ' = ' Ota township ' ; Array (' id ' = +, ' pid ' = + ', ' name ' = ' righteousness village '), ' + ' array (' id ' = ' + ', ' pid ' = ' + '), ' name ' = ' ' Shang Ba Village ' '),;p Rint_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          ([ID] + 3 [pid] = 1 [name] = Nanchang) [1] = = Array         ([id] = 13   [PID] = 1 [name] = = Ganzhou [son] = = Array ([0] = = Array                    ([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] = Shang BaVillage))) [1] = A                          Rray ([id] [+] [pid] = 14 [Name] = Ota township)) [1] =&gt ; Array ([id] = [pid] = [Name] =&gt ; Yudou County))) [1] = = Array ([id] + 2 [pid] = 0 [nam  E] = Heilongjiang province [son] + array ([0] = = Array ([ID] + 4 [pid] = =                  2 [Name] = Harbin City [son] = array ([0] = = Array ( [id] = 6 [PID] + 4 [name] + Xiangfang District [son] =&gt ;             Array       ([0] = = Array ([id] = 8                          [PID] = 6 [name] = and Xing Lu [son] = Array                              ([0] = = Array ([id] = 10                              [PID] = 8 [name] = Northeast Forestry University                              ) [1] = = Array (                              [ID] [PID] + 8 [name] = =                     Harbin Normal University)) )) [1] = = Array ([id] = 7 [            PID] = 4      [Name] = Nangang District [son] = array ([0] = = Array                       ([id] = 9 [PID] + 7 [name] = West Tai Straight Street                          [Son] = array ([0] = = Array ([id] = [pid] = 9 [name] =&G                           T                 Harbin Institute of Technology)))            ))) [1] = = Array ([id] + 5 [pid] = 2 [Name] = Jixi))) */


Extremely simple and effective!!! Very good!

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.