Home >Backend Development >PHP Tutorial >PHP unlimited classification_PHP tutorial

PHP unlimited classification_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:08:40899browse

CI's seven provided me with a method, as follows:

[php]
/**
* 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();
} www.2cto.com
/**
* Format data into a tree structure
* @author Xuefen.Tong
* @param array $items
* @return array
*/
function genTree9($items) {
$tree = array(); //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 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' => 'Gan 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' => 'Dataan 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                                  
            (                                                
                                                                                     
                  [pid] => 1  
                                                                                                                                                                                   
            )                                                
                    [1] => Array                                      
            (                                                 >
                  [pid] => 1  
                                                                                                                                                                         
                    [son] =>
                        (                  
                                                                                                                                                                     
                              (           > > > Gan County [son] =>
                                                            (              
                                                                                                                                                                                    
                                                                                                        (        
                                                                                                                                           
                                                                                                                                                                                                  
                                                                                                                                                                                             
                                                                                                                                                                                        
                                                                                          
                                                                                                                                                                          
                                                                                         
                                                            [id] => 18  
                                                            [pid] => 16  
                                                            [name] => 义源村  
                                                            )  
  
                                                        [1] => Array  
                                                            (  
                                                            [id] => 19  
                                                            [pid] => 16  
                                                            [name] => 上坝村  
                                                            )  
  
                                                        )  
  
                                                )  
  
                                            [1] => Array  
                                                (  
                                                    [id] => 17  
                                                    [pid] => 14  
                                                    [name] => 大田乡  
                                                )  
  
                                            )  
  
                                    )  
  
                                [1] => Array  
                                    (  
> > >
                                                                                                                                 
                                                                                               
            )                                                
      ) 
)
[1] => Array
(
[id] => 2
[pid] => 0
[name] => Heilongjiang Province
[son] => Array
        (                                    
                [0] => Array                                  
            (                                                
                                                                             
                  [pid] => 2 > Harbin
                  [son] =>
                      (                
                                                                                                                                                                                                         
                        (                                                        
                                                                                       
                                                                                                                                                                       
                                                                                                                                           
                                                                                                                                             
                                                    (              
                                                                                                                                                                                               
                                                            (              
                                                                                                                                              
                                                                                                                                                                                       
                                                                                                                               
                                                                                                                                        
                                                                                                                                           
                                                                                                                                                                        
                                                                                         
                                                                                                                          
                                                                                                                                                        
                                                                                                                  
Northeast Forestry University
                                                                                                                                 
                                                                                                                                                                            
                                                                                         
                                                                                                                                 
                                                                                                                                                      
                                                                                                                  
Harbin Normal University
                                                                                                                                 
                                                                                                                                                              
                                                                                                                                  
                                                                                                                                         
                                                                                                               
[1] =>
                        (                                                        
                                                                                         
                                                                                                                                                                       
                                                                                                                                                                 
                                    [son] => Array  
                                        (  
                                        [0] => Array  
                                            (  
                                            [id] => 9  
                                            [pid] => 7  
                                            [name] => 西大直街  
                                            [son] => Array  
                                                (  
                                                [0] => Array  
                                                    (  
                                                    [id] => 11  
                                                    [pid] => 9  
                                                    [name] =>   
                                                     哈尔滨工业大学  
                                                    )  
  
                                                )  
  
                                            )  
  
                                        )  
  
                                )  
  
                            )  
  
                    )  
  
                [1] => Array  
                    (  
                        [id] => 5  
                        [pid] => 2  
                        [name] => 鸡西市  
                    )  
  
            )  
  
    )  
)*  
 
Through the test, the imagined infinite levels of classification can be completed. However, it can be used with its own data, but not with the data in my database. Errors will occur, such as the following error:
[php]
Array
(
[10] => Array
(
[child] => Array
          (                                                          
                                                    [14] =>
          )                                    
) )
[11] =>
[12] =>
[9] => Array
(
[child] => Array
          (                                                          
                                                      [13] =>
          )                                    
) )
)
Only displays the array serial number and does not display the corresponding data. This is very distressing to me. This way it can be fully displayed after testing:
[php]
function genTree5($items) {
foreach ($items as $item) {
$items[$item['id']]=$item;//ADD THIS
$items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];
} }
return isset($items[0]['son']) ? $items[0]['son'] : array();
}
But this can only be used in ascending id order. If the order is out of order, an error will be displayed
Although the ids will remain in ascending order in most cases, if it involves sorting, the pass cannot be used.
Previously, Mr. Lu from CI provided an efficient method, namely:
[php]
id parent id rating
1 0 0, 1
2 1 0, 1, 2
3 1 0, 1, 3
4 3 0, 1, 3, 4
select * from table order by grade asc
This structure will sort the grades in parent-child order. First, sort the first 0, then sort the second 1, and so on. Automatic classification is very easy to use (note at the same time, be sure to keep two groups , that is, starting from 0,1 or starting from 0,0, otherwise the root level will be displayed in advance). "Classification" can also be called childid, because it is the sub-id layer of the record.
[php]
|--2
|----2-1
|------2-1-1
|--3
|----3-1
|--4
|--1
|----1-1
The display can be traversed directly, because I have a depth field to record the depth, so it can be displayed better.
The running time of this method is also short, running in microseconds. In short, it is spent on sorting the database, and the rest is basically very enjoyable.
Using pointers, the test without cache is generally greater than the time after sorting, mostly CPU time, and the results are inaccurate.
PS: PHP has weakened concepts such as addresses and pointers. In other words, it is not a feature of PHP. Although it can be used, some inexplicable errors will occur. So let’s be honest ~ Shenma big data algorithm does not need to be done in PHP. C++ or something like this is much faster than PHP. . . .
Meanwhile @Tonton makes corrections to his algorithm.
http://www.bkjia.com/PHPjc/477766.html
www.bkjia.com
true

http: //www.bkjia.com/PHPjc/477766.html

CI’s seven provided me with a method as follows: [php] ?php /*** This method is provided by @Tonton * http://my.oschina.net/u/918697 * @date 2012-12-12* / function genTree5($items) { fore...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn