We know that many open source software’s infinite classifications use recursive algorithms, but we know that recursion wastes time and space (memory).
Last time I also shared my own original infinite classification spanning tree method. An enthusiastic PHP expert netizen gave me valuable suggestions. I tested it and found that the time of this code is very short. Reference:
http://www.oschina.net/code/snippet_98719_11296, I sorted it out again and found that after querying the data in the database, we have already set the key value, so in practice, we usually query the formatted main value in the model. The key value corresponds to the form of the data, so we can use such data directly, eliminating a layer of loops. 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(); //格式化好的树
- 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 Province'),
- 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 City'),
- 5 = > array('id' => 5, 'pid' => 2, 'name' => 'Jixi City'),
- 6 => array('id' => 6, 'pid' => 4, 'name' => 'Xiangfang District'),
- 7 => array('id' => 7, 'pid' => 4, 'name' => 'Nangang District '),
- 8 => array('id' => 8, 'pid' => 6, 'name' => 'Hexing Road'),
- 9 => array('id' = > 9, 'pid' => 7, 'name' => 'Xidazhi Street'),
- 10 => array('id' => 10, 'pid' => 8, ' name' => 'Northeast Forestry University'),
- 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' => 'Ganzhou County'),
- 15 => array('id' => 15, 'pid' => 13, 'name' => 'Yudu County'),
- 16 => array('id' => 16, 'pid' => 14, 'name' => 'Maodian Town'),
- 17 => array('id' => 17, 'pid' => 14, ' name' => 'Datian Township'),
- 18 => array('id' => 18, 'pid' => 16, 'name' => 'Yiyuan Village'),
- 19 = > array('id' => 19, 'pid' => 16, 'name' => 'Shangba Village'),
- );
- echo "
";
- print_r(genTree5( $items));
- print_r(genTree9($items));
-
- //The latter output format is similar to the former, except that the array key values are different, but it 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 City
- [son] => Array
- (
- [0] => Array
- (
- [id] => 14
- [pid] => 13
- [name] => Gan County
- [son] => Array
- (
- [0] => > Maodian Town
- [son] => Array
- (
- [0] => Array
- (
- [id] => 18
- [pid] => 16
- [name] => Yiyuan Village
- )
-
- [1] => Array
- (
- [id] => 19
- [pid] => [1] => Array
- (
- [id] => 17
- [pid] => 14
- [name] => Datian 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] => Hexing Road
- [son] => Array
- (
- [0] => Array
- (
- [id] => 10
- [pid] => 8
- [name] =>
- Northeast Forestry University
- )
-
- [1] => Array
- ( Array
- (
- [id] => 7
- [pid] => 4
- [name] => Nangang District
- [son] => Array
- (
- [0] => Array
- (
- [id ] => 9
- [pid] => 7
- [name] => Array Array
- (
- [id] => 5
- [pid] => 2
- [name] => Jixi City
- )
-
- )
-
- )
- )*/
-
Copy code
|