We know that a lot of open source software infinite classification is a recursive algorithm, but we know that recursion is a waste of time, but also a waste of space (memory),
Last time I shared my own original infinite classification spanning tree method, an enthusiastic PHP expert Netizen gave me valuable suggestions, I tested a bit, this code time very short reference: http://www.oschina.net/code/snippet_98719_ 11296, I sorted it again, found that the database query data, we have set the key value, so in practice, we generally query in model format into the primary key value of the corresponding data form, so we can directly use such data, there is less than a layer of circulation. The code is also very concise.
The original code:
function Gentree ($items, $id = ' id ', $pid = ' pid ', $son = ' children ') {$tree = array ();//format 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] [] = & $tmpM
ap[$item [$id]];
else {$tree [] = & $tmpMap [$item [$id]];
} unset ($TMPMAP);
return $tree; $items 1 = Array (array (' ID ' => 1, ' pid ' => 0, ' name ' => ' Level One '), array (' ID ' =>, ' pid ' => 0, ' Name ' => '), array (' ID ' => 2, ' pid ' => 1, ' name ' => ' Level two '), array (' ID ' =>, ' pid ' => One, ' name ' => ' two '), array (' ID ' => 3, ' pid ' => 1, ' name ' => ' two rank '), array (' ID ' =>, ' pid ' = >, ' name ' => ' Level two '), array (' ID ' => 9, ' pid ' => 1, ' name ' => ' two '), array (' ID ' =>, ' pi d ' => 1, ' name ' => ' Level two '), array (' ID ' => 4, ' pid ' => 9, ' name ' => ' Level three '), array (' ID ' => 6, ' PID ' => 9, ' name ' => ' three level '), array (' ID ' => 7, ' PID ' => 4, ' name ' => ; ' Level four '), array (' ID ' => 8, ' PID ' => 4, ' name ' => ' four class '), array (' ID ' => 5, ' pid ' => 4, ' name ' => ; ' Level four '), array (' ID ' =>, ' pid ' => 4, ' name ' => ' four class '), array (' ID ' =>, ' pid ' => 8, ' name ' =& Gt ' Level five '), array (' ID ' =>, ' pid ' => 8, ' name ' => ' five level '), array (' ID ' =>, ' pid ' => 8, ' name ' =& Gt
' Level five '), array (' ID ' =>, ' pid ' =>, ' name ' => ' Level 64 '),); Var_dump (Gentree ($items 1));
The following are supplementary:
/** *
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 ();
}
/** * Formats the data into a tree structure * @author xuefen.tong * @param array $items * @return Array */function GenTree9 ($items) {$tree = Array (); A well-formed tree foreach ($items as $item) if (Isset ($items [$item [' pid]]) $items [$item [' pid ']][' son '] = & $item
s[$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 '), 6 => Array ( ' ID ' => 6, ' PID ' => 4, ' name ' => ' Xiangfang District '), 7 => array (' ID ' => 7, ' PID ' => 4, ' name ' => ' Nangang '), 8 =& Gt
Array (' ID ' => 8, ' pid ' => 6, ' name ' => ' and Hing Road '), 9 => array (' ID ' => 9, ' PID ' => 7, ' name ' => ' West big Straight Street '), => Array (' ID ' =>, ' pid ' =>8, ' name ' => ' Nefu '), one => array (' ID ' =>, ' pid ' => 9, ' name ' => ' Harbin Institute of Technology '), => array (' id ' = >, ' PID ' => 8, ' name ' => ' Harbin Normal University '), => array (' ID ' =>, ' pid ' => 1, ' name ' => ' Ganzhou '), 14 => Array (' ID ' =>, ' pid ' =>, ' name ' => ' Gan County '), => array (' ID ' =>, ' pid ' =>, ' name ' => ; ' Yudu '), => array (' ID ' =>, ' pid ' =>, ' name ' => ' Mao Dian Zhen '), => array (' ID ' =>, ' pid ' => 1 4, ' name ' => ' da Tian Xiang '), => array (' ID ' =>, ' pid ' =>, ' name ' => ' yi Yuan Cun '), => array (' ID ' => 1
9, ' PID ' =>, ' name ' => ' Shang Cun ');
Print_r (GenTree5 ($items));
Print_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 Province
[Son] => Array ([0] => array ([ID] => 3 [PID] => 1
[Name] => Nanchang City)[1] => Array ([id] => [PID] => 1 [name] => Ganzhou [
Son] => Array ([0] => array ([ID] => 14
[PID] => [name] => Gan County [son] => Array
([0] => Array ([id] => 16
[PID] => [name] => Mao Dian Zhen [son] => Array
([0] => Array (
[id] => [PID] => [name] => Yi Yuan Cun
) [1] => Array ( [ID] => 19 [PID] => [name] => Shangba Village
)) [1] => Array ([id] => [PID] => [name]
=> da Tian Xiang)) [1] => Array
([id] => [PID] => [name] => Yudu ))) [1] => Array ([id] => 2 [PID] =>
0 [name] => Heilongjiang province [son] => Array ([0] => array ([ID] => 4 [PID] => 2 [name] => Harbin [son] => Array ([0] => Ar
Ray ( [ID] => 6 [PID] => 4 [name] => Xiangfang District [son] =& Gt Array ([0] => array ([ID] => 8 [PID] => 6 [name] => and Hing Road [son] => Arra
Y ([0] => Array (
[id] => [PID] => 8 [name] =>
NEFU) [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] => sai da zhi jie [son] => Array
([0] => Array ([id] => 11
[PID] => 9 [name] => Harbin Institute of Technology
)
)
)
)
)
) [1] => Array ([id] => 5 [PID] => 2
[Name] => Jixi )
)
)
)*/
Extremely simple and effective!!! Very good!