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 is 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.
-
- /**
- * 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 (
- 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 '),
- 4 = = Array (' id ' = = 4, ' pid ' = 2, ' name ' = ' Harbin City '),
- 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 = = Array (' id ' = = 8, ' pid ' = 6, ' name ' = ' and Hing Road '),
- 9 = = Array (' id ' = = 9, ' pid ' = ' 7 ', ' name ' = ' West Dazhi street '),
- The array (' id ' = +, ' pid ' = 8, ' name ' = ' Northeast Forestry University '),
- One-by-one and array (' id ' = = ', ' pid ' = + 9, ' name ' = ' Harbin Institute of Technology '),
- The array (' id ' = +, ' pid ' = + 8, ' name ' = ' harbin Normal University '),
- The array (' id ' = ' + ', ' pid ' = + 1, ' name ' = ' Ganzhou '),
- The array (' id ' = ' + ', ' pid ' = ' = ', ' name ' = ' ' Gan County '),
- The array (' id ' = +, ' pid ' = ' + ', ' name ' = ' ' Yudu County '),
- A + = array (' id ' = ' + ', ' pid ' = ' + ', ' name ' = ' Mao Shop town '),
- + = array (' id ' = +, ' pid ' = ' = ', ' name ' = ' Ota township '),
- The array (' id ' = ' + ', ' pid ' = ' + ', ' name ' = ' yi Yuan cun '),
- The array (' id ' = +, ' pid ' = ' + ', ' name ' = ' ' Shang Ba Village '),
- );
- echo "
"; - 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] = 13
- [PID] = 1
- [Name] = Ganzhou
- [Son] = Array
- (
- [0] = = Array
- (
- [id] = 14
- [PID] = 13
- [Name] = Gan County
- [Son] = Array
- (
- [0] = = Array
- (
- [id] = 16
- [PID] = 14
- [Name] + Mao Dian Zhen
- [Son] = Array
- (
- [0] = = Array
- (
- [id] = 18
- [PID] = 16
- [Name] and righteousness Source Village
- )
- [1] = = Array
- (
- [id] = 19
- [PID] = 16
- [Name] = Shangba Village
- )
- )
- )
- [1] = = Array
- (
- [id] = 17
- [PID] = 14
- [Name] = Ota Township
- )
- )
- )
- [1] = = Array
- (
- [id] = 15
- [PID] = 13
- [Name] = Yudu County
- )
- )
- )
- )
- )
- [1] = = Array
- (
- [id] = 2
- [PID] = 0
- [Name] = 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] = Array
- (
- [0] = = Array
- (
- [id] = 8
- [PID] = 6
- [name]/= Hing 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] = West Tai Straight Street
- [Son] = Array
- (
- [0] = = Array
- (
- [id] = 11
- [PID] = 9
- [Name] + =
- Harbin Institute of Technology
- )
- )
- )
- )
- )
- )
- )
- [1] = = Array
- (
- [id] = 5
- [PID] = 2
- [Name] = Jixi
- )
- )
- )
- )*/
Copy Code |