Home  >  Article  >  Backend Development  >  Detailed explanation of PHP Infinitus classification

Detailed explanation of PHP Infinitus classification

小云云
小云云Original
2018-03-20 13:09:196810browse

Infinitus classification simply means that a category can be divided into multiple subcategories, and then one subcategory can be divided into multiple other subcategories and so on indefinitely, just like Windows can create a new folder, and then in this folder You can also create some folders, and you can also create some folders under the folders.

So how does PHP achieve its infinite classification? How to list its various categories one by one?

First, let’s assume there is such an array

$arr = array(    0=>array(        'cid'=>1,        'pid'=>0,        'name'=>'亚洲',
    ),    1=>array(        'cid'=>2,        'pid'=>0,        'name'=>'北美洲',
    ),    2=>array(        'cid'=>3,        'pid'=>1,        'name'=>'中国',
    ),    3=>array(        'cid'=>4,        'pid'=>2,        'name'=>'美国',
    ),    4=>array(        'cid'=>5,        'pid'=>3,        'name'=>'北京',
    ),    5=>array(        'cid'=>6,        'pid'=>3,        'name'=>'河北',
    ),    6=>array(        'cid'=>7,        'pid'=>5,        'name'=>'东城区',
    ),    7=>array(        'cid'=>8,        'pid'=>5,        'name'=>'海淀区',
    ),
);

We need a display similar to this as shown
Detailed explanation of PHP Infinitus classification

If we want to If this array is displayed intuitively as shown in the picture above, it needs to be implemented using PHP recursion. So how to implement it? Look at the core code below

Copy code

private function GetTree($arr,$pid,$step){
    global $tree;    foreach($arr as $key=>$val) {        if($val['pid'] == $pid) {            $flg = str_repeat('└―',$step);            $val['name'] = $flg.$val['name'];            $tree[] = $val;            $this->GetTree($arr , $val['cid'] ,$step+1);
        }
    }    return $tree;
}

Copy code
Then we only need to write a calling code

$newarr = $this->GetTree($arr, 0, 0);

Find out all the subclass ids Parent classification information (in yii2 framework)

Method 1:

public static function get_parent_list($arr,$id){
        //$arr 所有分类列表
        //$id 父级分类id
        static $list=array();        foreach($arr as $u){            if($u['id']== $id){//父级分类id等于所查找的id
                $list[]=$u;                if($u['parent_id']>0){                    self::get_parent_list($arr,$u['parent_id']);

                }
            }
        }       return $list;
    }

This way, classification information can be stored in the cache without recursive query.
Method 2: Recursive query to obtain classification information

    public static function get_parents($id){
        static $list = [];        $cat_data = Category::findOne($id)->toarray();        if($cat_data){            $list[] = $cat_data;                    $id = $cat_data['parent_id'];                    if($cat_data['parent_id'] > 0){                            self::get_parents($id);
                    }
        }        return $list;
    }

Since we need to achieve infinite classification, we need to know what its sub-columns are?

The final array implemented is in the form:

[php] view plain copyArray  (  
    [0] => Array  
        (  
            [category_id] => 1  
            [category_name] => 关于我  
            [category_pid] => 0  
            [category_addtime] => 0  
            [category_order] => 0  
        )  

    [1] => Array  
        (  
            [category_id] => 2  
            [category_name] => 生活随笔  
            [category_pid] => 0  
            [category_addtime] => 0  
            [category_order] => 0  
        )  

    [2] => Array  
        (  
            [category_id] => 3  
            [category_name] => 文章类别  
            [category_pid] => 0  
            [category_addtime] => 0  
            [category_order] => 0  
            [category_child] => Array  
                (  
                    [0] => Array  
                        (  
                            [category_id] => 4  
                            [category_name] => Linux服务器  
                            [category_pid] => 3  
                            [category_addtime] => -28800  
                            [category_order] => 1  
                            [category_child] => Array  
                                (  
                                    [0] => Array  
                                        (  
                                            [category_id] => 5  
                                            [category_name] => linux优化  
                                            [category_pid] => 4  
                                            [category_addtime] => 0  
                                            [category_order] => 0  
                                        )  

                                    [1] => Array  
                                        (  
                                            [category_id] => 6  
                                            [category_name] => 虚拟化  
                                            [category_pid] => 4  
                                            [category_addtime] => 1478228220  
                                            [category_order] => 0  
                                        )  

                                    [2] => Array  
                                        (  
                                            [category_id] => 7  
                                            [category_name] => Mysql优化与开发  
                                            [category_pid] => 4  
                                            [category_addtime] => 1478188800  
                                            [category_order] => 0  
                                        )  

                                    [3] => Array  
                                        (  
                                            [category_id] => 8  
                                            [category_name] => 高可用与虚拟化  
                                            [category_pid] => 4  
                                            [category_addtime] => 1478394120  
                                            [category_order] => 0  
                                        )  

                                )  

                        )  

                    [1] => Array  
                        (  
                            [category_id] => 15  
                            [category_name] => Mysql  
                            [category_pid] => 3  
                            [category_addtime] => 1480555980  
                            [category_order] => 0  
                            [category_child] => Array  
                                (  
                                    [0] => Array  
                                        (  
                                            [category_id] => 16  
                                            [category_name] => 优化  
                                            [category_pid] => 15  
                                            [category_addtime] => 1480555980  
                                            [category_order] => 0  
                                        )  

                                    [1] => Array  
                                        (  
                                            [category_id] => 17  
                                            [category_name] => Sql编程  
                                            [category_pid] => 15  
                                            [category_addtime] => 1480556040  
                                            [category_order] => 8  
                                        )  

                                )  

                        )  

                )  

        )  

)

So how can the two-dimensional array we take out from the database become an array like the above?
Example:

We want to implement the following two-dimensional array into the above form.

[php] view plain copy

$arr=array(    
    array('id'=>'1','name'=>'北京','pid'=>'0'),    
    array('id'=>'2','name'=>'上海','pid'=>'0'),    
    array('id'=>'3','name'=>'浦东','pid'=>'2'),    
    array('id'=>'4','name'=>'朝阳','pid'=>'1'),    
    array('id'=>'5','name'=>'广州','pid'=>'0'),    
    array('id'=>'6','name'=>'三里屯','pid'=>'4'),   
    array('id'=>'7','name'=>'广东','pid'=>'5'),   
    array('id'=>'8','name'=>'三里','pid'=>'4'),  
    array('id'=>'10','name'=>'小胡同','pid'=>'8')  
    );

The first method: Use the index of the array to be the same as the id number of the primary key to find the sub-column. First add the index to the array:

[php] view plain copy

$arr=array(    
    1=>array('id'=>'1','name'=>'北京','pid'=>'0'),    
    2=>array('id'=>'2','name'=>'上海','pid'=>'0'),    
    3=>array('id'=>'3','name'=>'浦东','pid'=>'2'),    
    4=>array('id'=>'4','name'=>'朝阳','pid'=>'1'),    
    5=>array('id'=>'5','name'=>'广州','pid'=>'0'),    
    6=>array('id'=>'6','name'=>'三里屯','pid'=>'4'),   
    7=>array('id'=>'7','name'=>'广东','pid'=>'5'),   
    8=>array('id'=>'8','name'=>'三里','pid'=>'4'),  
    10=>array('id'=>'10','name'=>'小胡同','pid'=>'8')  
    );

Use the subscript of the index to determine whether the column has a parent column. If it exists, put the column into the son array of the parent column
[php] view plain copy

function generateTree($items){  
    $tree = array();  
    foreach($items as $item){  
        //判断是否有数组的索引==  
        if(isset($items[$item['pid']])){     //查找数组里面是否有该分类  如 isset($items[0])  isset($items[1])  
            $items[$item['pid']]['son'][] = &$items[$item['id']]; //上面的内容变化,$tree里面的值就变化  
        }else{  
            $tree[] = &$items[$item['id']];   //把他的地址给了$tree  
        }    
    }  
    return $tree;  
}

There is a major problem with the above program. Did we add the index of the array manually? So can we automatically add array indexes? Of course you can. The following program can be implemented. It automatically adds an index to the array and then puts the sub-column into the son array of the parent column. The idea is the same as the above program.

[php] view plain copy

function make_tree($list,$pk='id',$pid='pid',$child='_child',$root=0){  
    $tree=array();   
    $packData=array();  
    foreach ($list as  $data) {  

    <span style="white-space:pre">    </span>//转换为带有主键id的数组  
        $packData[$data[$pk]] = $data; //$packData[1]=$data; $packData[2]=$data   
    }  
    foreach ($packData as $key =>$val){       
        if($val[$pid]==$root){   //代表跟节点         
            $tree[]=& $packData[$key];  
        }else{  
            //找到其父类  
            $packData[$val[$pid]][$child][]=& $packData[$key];  
        }  
    }  
    return $tree;  
}

Second method: Use recursion to find sub-columns
Idea: Use recursion to find the sub-columns in each column, until the last sub-column is found, and then run the debugging layer by layer, You can print

tree. To facilitate understanding, you can print $tree to observe changes in the array.

[php] view plain copy

array(&#39;id&#39;=>&#39;6&#39;,&#39;name&#39;=>&#39;三里屯&#39;,&#39;pid&#39;=>&#39;4&#39;),   
[php] view plain copyfunction make_tree1($list,$pk=&#39;id&#39;,$pid=&#39;pid&#39;,$child=&#39;_child&#39;,$root=0){  
    $tree=array();  
    foreach($list as $key=> $val){  
        if($val[$pid]==$root){  
            //获取当前$pid所有子类   
                unset($list[$key]);  
                if(! empty($list)){  
                    $child=make_tree1($list,$pk,$pid,$child,$val[$pk]); //来来来 找北京的子栏目 递归 空  

                    if(!empty($child)){  
                        $val[&#39;_child&#39;]=$child;  
                    }  
                }                
                $tree[]=$val;  
        }  
    }     
    return $tree;  
}

In order to better understand recursion, you can find a small program to test, such as the following summation program
You can analyze the output of $n from 2, 3...10, instead of From 10, 9--2, I only understood how the recursion of the above program is implemented through this program. In short: recursion is a very interesting thing, I hope everyone can understand it well
[php] view plain copy

        function sum($n){  
            $a=0;  
            if($n>1){     
                $a=sum($n-1)+$n;  

                /*if($n>5){ 
                    echo "$n <br />" ; 
                }*/  
                echo "$n<br />";  
            }else{  
                $a=1;  
            }  

            return $a;  
        }  
        echo sum(10);

Get the data of all subordinate subclass ids based on the parent id

#$id = 父级id, $array = 所有分类public function getSon($id,$array){
        static $list;        foreach ($array as $k => $v) {            if($v[&#39;parent_id&#39;] == $id){                $list[] = $array[$k];                self::getSon($v[&#39;id&#39;],$array);
            }
        }        return $list;
}

         

php Infinitus Classification
Here is the first introduction , what is the infinite classification?

Infinitus Classification To put it simply, a category can be divided into multiple subcategories, and then one subcategory can be divided into multiple other subcategories and so on indefinitely, just like Windows can create a new folder, and then in this You can create some folders in the folder, and you can also create some folders under the folder.

So how does PHP achieve its infinite classification? How to list its various categories one by one?

First, let’s assume there is such an array

$arr = array(    0=>array(        &#39;cid&#39;=>1,        &#39;pid&#39;=>0,        &#39;name&#39;=>&#39;亚洲&#39;,
    ),    1=>array(        &#39;cid&#39;=>2,        &#39;pid&#39;=>0,        &#39;name&#39;=>&#39;北美洲&#39;,
    ),    2=>array(        &#39;cid&#39;=>3,        &#39;pid&#39;=>1,        &#39;name&#39;=>&#39;中国&#39;,
    ),    3=>array(        &#39;cid&#39;=>4,        &#39;pid&#39;=>2,        &#39;name&#39;=>&#39;美国&#39;,
    ),    4=>array(        &#39;cid&#39;=>5,        &#39;pid&#39;=>3,        &#39;name&#39;=>&#39;北京&#39;,
    ),    5=>array(        &#39;cid&#39;=>6,        &#39;pid&#39;=>3,        &#39;name&#39;=>&#39;河北&#39;,
    ),    6=>array(        &#39;cid&#39;=>7,        &#39;pid&#39;=>5,        &#39;name&#39;=>&#39;东城区&#39;,
    ),    7=>array(        &#39;cid&#39;=>8,        &#39;pid&#39;=>5,        &#39;name&#39;=>&#39;海淀区&#39;,
    ),
);

We need a display similar to this as shown
Detailed explanation of PHP Infinitus classification

If we want to If this array is displayed intuitively as shown in the picture above, it needs to be implemented using PHP recursion. So how to implement it? Look at the core code below

Copy code

private function GetTree($arr,$pid,$step){
    global $tree;    foreach($arr as $key=>$val) {        if($val[&#39;pid&#39;] == $pid) {            $flg = str_repeat(&#39;└―&#39;,$step);            $val[&#39;name&#39;] = $flg.$val[&#39;name&#39;];            $tree[] = $val;            $this->GetTree($arr , $val[&#39;cid&#39;] ,$step+1);
        }
    }    return $tree;
}

Copy code
Then we only need to write a calling code

$newarr = $this->GetTree($arr, 0, 0);

Find out all the subclass ids Parent classification information (in yii2 framework)

Method 1:

public static function get_parent_list($arr,$id){
        //$arr 所有分类列表
        //$id 父级分类id
        static $list=array();        foreach($arr as $u){            if($u[&#39;id&#39;]== $id){//父级分类id等于所查找的id
                $list[]=$u;                if($u[&#39;parent_id&#39;]>0){                    self::get_parent_list($arr,$u[&#39;parent_id&#39;]);

                }
            }
        }       return $list;
    }

This way, classification information can be stored in the cache without recursive query.
Method 2: Recursive query to obtain classification information

    public static function get_parents($id){
        static $list = [];        $cat_data = Category::findOne($id)->toarray();        if($cat_data){            $list[] = $cat_data;                    $id = $cat_data[&#39;parent_id&#39;];                    if($cat_data[&#39;parent_id&#39;] > 0){                            self::get_parents($id);
                    }
        }        return $list;
    }

Since we need to achieve infinite classification, we need to know what its sub-columns are?

The final array implemented is in the form:

[php] view plain copyArray  (  
    [0] => Array  
        (  
            [category_id] => 1  
            [category_name] => 关于我  
            [category_pid] => 0  
            [category_addtime] => 0  
            [category_order] => 0  
        )  

    [1] => Array  
        (  
            [category_id] => 2  
            [category_name] => 生活随笔  
            [category_pid] => 0  
            [category_addtime] => 0  
            [category_order] => 0  
        )  

    [2] => Array  
        (  
            [category_id] => 3  
            [category_name] => 文章类别  
            [category_pid] => 0  
            [category_addtime] => 0  
            [category_order] => 0  
            [category_child] => Array  
                (  
                    [0] => Array  
                        (  
                            [category_id] => 4  
                            [category_name] => Linux服务器  
                            [category_pid] => 3  
                            [category_addtime] => -28800  
                            [category_order] => 1  
                            [category_child] => Array  
                                (  
                                    [0] => Array  
                                        (  
                                            [category_id] => 5  
                                            [category_name] => linux优化  
                                            [category_pid] => 4  
                                            [category_addtime] => 0  
                                            [category_order] => 0  
                                        )  

                                    [1] => Array  
                                        (  
                                            [category_id] => 6  
                                            [category_name] => 虚拟化  
                                            [category_pid] => 4  
                                            [category_addtime] => 1478228220  
                                            [category_order] => 0  
                                        )  

                                    [2] => Array  
                                        (  
                                            [category_id] => 7  
                                            [category_name] => Mysql优化与开发  
                                            [category_pid] => 4  
                                            [category_addtime] => 1478188800  
                                            [category_order] => 0  
                                        )  

                                    [3] => Array  
                                        (  
                                            [category_id] => 8  
                                            [category_name] => 高可用与虚拟化  
                                            [category_pid] => 4  
                                            [category_addtime] => 1478394120  
                                            [category_order] => 0  
                                        )  

                                )  

                        )  

                    [1] => Array  
                        (  
                            [category_id] => 15  
                            [category_name] => Mysql  
                            [category_pid] => 3  
                            [category_addtime] => 1480555980  
                            [category_order] => 0  
                            [category_child] => Array  
                                (  
                                    [0] => Array  
                                        (  
                                            [category_id] => 16  
                                            [category_name] => 优化  
                                            [category_pid] => 15  
                                            [category_addtime] => 1480555980  
                                            [category_order] => 0  
                                        )  

                                    [1] => Array  
                                        (  
                                            [category_id] => 17  
                                            [category_name] => Sql编程  
                                            [category_pid] => 15  
                                            [category_addtime] => 1480556040  
                                            [category_order] => 8  
                                        )  

                                )  

                        )  

                )  

        )  

)

So how can the two-dimensional array we take out from the database become an array like the above?
Example:

We want to implement the following two-dimensional array into the above form.

[php] view plain copy

$arr=array(    
    array(&#39;id&#39;=>&#39;1&#39;,&#39;name&#39;=>&#39;北京&#39;,&#39;pid&#39;=>&#39;0&#39;),    
    array(&#39;id&#39;=>&#39;2&#39;,&#39;name&#39;=>&#39;上海&#39;,&#39;pid&#39;=>&#39;0&#39;),    
    array(&#39;id&#39;=>&#39;3&#39;,&#39;name&#39;=>&#39;浦东&#39;,&#39;pid&#39;=>&#39;2&#39;),    
    array(&#39;id&#39;=>&#39;4&#39;,&#39;name&#39;=>&#39;朝阳&#39;,&#39;pid&#39;=>&#39;1&#39;),    
    array(&#39;id&#39;=>&#39;5&#39;,&#39;name&#39;=>&#39;广州&#39;,&#39;pid&#39;=>&#39;0&#39;),    
    array(&#39;id&#39;=>&#39;6&#39;,&#39;name&#39;=>&#39;三里屯&#39;,&#39;pid&#39;=>&#39;4&#39;),   
    array(&#39;id&#39;=>&#39;7&#39;,&#39;name&#39;=>&#39;广东&#39;,&#39;pid&#39;=>&#39;5&#39;),   
    array(&#39;id&#39;=>&#39;8&#39;,&#39;name&#39;=>&#39;三里&#39;,&#39;pid&#39;=>&#39;4&#39;),  
    array(&#39;id&#39;=>&#39;10&#39;,&#39;name&#39;=>&#39;小胡同&#39;,&#39;pid&#39;=>&#39;8&#39;)  
    );

The first method: use the index of the array to be the same as the id number of the primary key to find the sub-column, first add the index to the array:

[php] view plain copy

$arr=array(    
    1=>array(&#39;id&#39;=>&#39;1&#39;,&#39;name&#39;=>&#39;北京&#39;,&#39;pid&#39;=>&#39;0&#39;),    
    2=>array(&#39;id&#39;=>&#39;2&#39;,&#39;name&#39;=>&#39;上海&#39;,&#39;pid&#39;=>&#39;0&#39;),    
    3=>array(&#39;id&#39;=>&#39;3&#39;,&#39;name&#39;=>&#39;浦东&#39;,&#39;pid&#39;=>&#39;2&#39;),    
    4=>array(&#39;id&#39;=>&#39;4&#39;,&#39;name&#39;=>&#39;朝阳&#39;,&#39;pid&#39;=>&#39;1&#39;),    
    5=>array(&#39;id&#39;=>&#39;5&#39;,&#39;name&#39;=>&#39;广州&#39;,&#39;pid&#39;=>&#39;0&#39;),    
    6=>array(&#39;id&#39;=>&#39;6&#39;,&#39;name&#39;=>&#39;三里屯&#39;,&#39;pid&#39;=>&#39;4&#39;),   
    7=>array(&#39;id&#39;=>&#39;7&#39;,&#39;name&#39;=>&#39;广东&#39;,&#39;pid&#39;=>&#39;5&#39;),   
    8=>array(&#39;id&#39;=>&#39;8&#39;,&#39;name&#39;=>&#39;三里&#39;,&#39;pid&#39;=>&#39;4&#39;),  
    10=>array(&#39;id&#39;=>&#39;10&#39;,&#39;name&#39;=>&#39;小胡同&#39;,&#39;pid&#39;=>&#39;8&#39;)  
    );

利用索引的下标,判断该栏目是否存在父栏目,如果存在就把该栏目放到父栏目的son子数组里面
[php] view plain copy

function generateTree($items){  
    $tree = array();  
    foreach($items as $item){  
        //判断是否有数组的索引==  
        if(isset($items[$item[&#39;pid&#39;]])){     //查找数组里面是否有该分类  如 isset($items[0])  isset($items[1])  
            $items[$item[&#39;pid&#39;]][&#39;son&#39;][] = &$items[$item[&#39;id&#39;]]; //上面的内容变化,$tree里面的值就变化  
        }else{  
            $tree[] = &$items[$item[&#39;id&#39;]];   //把他的地址给了$tree  
        }    
    }  
    return $tree;  
}

上面这个程序有一个主要问题,数组的索引是我们手动添加的?那我们可不可以自动添加数组索引呢?当然可以,下面这个程序就实现了,自动为数组添加索引然后再把子栏目放到父栏目的son数组中,思想和上面的程序是一样的

[php] view plain copy

function make_tree($list,$pk=&#39;id&#39;,$pid=&#39;pid&#39;,$child=&#39;_child&#39;,$root=0){  
    $tree=array();   
    $packData=array();  
    foreach ($list as  $data) {  

    <span style="white-space:pre">    </span>//转换为带有主键id的数组  
        $packData[$data[$pk]] = $data; //$packData[1]=$data; $packData[2]=$data   
    }  
    foreach ($packData as $key =>$val){       
        if($val[$pid]==$root){   //代表跟节点         
            $tree[]=& $packData[$key];  
        }else{  
            //找到其父类  
            $packData[$val[$pid]][$child][]=& $packData[$key];  
        }  
    }  
    return $tree;  
}

第二种方法:使用递归来查找子栏目
思想:使用递归来查找每个栏目中的子栏目,直到找到最后一个子栏目以后再一层层往外运行调试的话,可以打印

tree中,,为了方便理解,可以打印$tree去观察数组的变化

[php] view plain copy

array(&#39;id&#39;=>&#39;6&#39;,&#39;name&#39;=>&#39;三里屯&#39;,&#39;pid&#39;=>&#39;4&#39;),   
[php] view plain copyfunction make_tree1($list,$pk=&#39;id&#39;,$pid=&#39;pid&#39;,$child=&#39;_child&#39;,$root=0){  
    $tree=array();  
    foreach($list as $key=> $val){  
        if($val[$pid]==$root){  
            //获取当前$pid所有子类   
                unset($list[$key]);  
                if(! empty($list)){  
                    $child=make_tree1($list,$pk,$pid,$child,$val[$pk]); //来来来 找北京的子栏目 递归 空  

                    if(!empty($child)){  
                        $val[&#39;_child&#39;]=$child;  
                    }  
                }                
                $tree[]=$val;  
        }  
    }     
    return $tree;  
}

为了更好的理解递归,大家可以找一个小程序测试一下例如下面这个求和的程序
大家可以分析一下$n的输出从2,3 …..10,而不是从10,9—-2,我从这个程序才理解了一下上面那个程序的递归是如何实现的,总之:递归是很有意思的一个东西,希望大家好好理解
[php] view plain copy

        function sum($n){  
            $a=0;  
            if($n>1){     
                $a=sum($n-1)+$n;  

                /*if($n>5){ 
                    echo "$n <br />" ; 
                }*/  
                echo "$n<br />";  
            }else{  
                $a=1;  
            }  

            return $a;  
        }  
        echo sum(10);

根据父id获得所有下级子类id的数据

#$id = 父级id, $array = 所有分类public function getSon($id,$array){
        static $list;        foreach ($array as $k => $v) {            if($v[&#39;parent_id&#39;] == $id){                $list[] = $array[$k];                self::getSon($v[&#39;id&#39;],$array);
            }
        }        return $list;
}

相关推荐:

php无限极分类生成分类树的实现方法介绍

php无限极分类入门教程

PHP无限极分类函数的实现方法实例详解

The above is the detailed content of Detailed explanation of PHP Infinitus classification. For more information, please follow other related articles on the PHP Chinese website!

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