Home > Backend Development > PHP Tutorial > PHP uses recursive method to achieve infinite classification

PHP uses recursive method to achieve infinite classification

大家讲道理
Release: 2023-03-05 18:10:02
Original
2368 people have browsed it

I believe that many friends will want to make a website to improve their skills when learning PHP, such as corporate websites and shopping mall websites. Their latest functions and column management use unlimited classification. method, we will next explore its deep implementation logic by studying the infinite-level classification technology.

What is infinite classification?

Infinite-level classification is a classification technique. For example, department organization, article classification, subject classification, etc. often use infinite-level classification. It can be simply understood as classification. In fact, if we think about it carefully, there are simply too many classifications in life. Clothes can be divided into men's clothing and women's clothing, tops and pants, and they can also be classified according to age groups. Classification is everywhere, and classification appears "infinite". I won’t talk about the necessity of infinite classification here.

Introduction to the Principle of Infinite Classification

Infinite classification may seem "high-level", but in fact the principle is very simple. Infinite classification not only requires the ingenuity of the code, but also relies on the rationality of the database design. To satisfy infinite classification, the database needs to have two necessary fields, id and pid. The id is used to identify itself, while the pid is used to indicate the parent id. In other words, each classification record not only describes itself, but also describes another ID that it is most closely concerned about. What seemed like a complicated matter was solved with such a little trick.

Example

Building database

Table name: category

id int primary key, auto-increment

name varchar classification name

pid int parent class id, default 0

The pid of the top category is 0 by default. When we want to take out the sub-category tree of a certain category, the basic idea is recursion. Of course, due to efficiency issues, it is not recommended to query the database every time recursively. The usual approach is to take out all categories first and save them in a PHP array. , then process it, and finally cache the results to improve the efficiency of the next request.
First build an original array, which can be pulled directly from the database:

$categories = array(
    array('id'=>1,'name'=>'电脑','pid'=>0),
    array('id'=>2,'name'=>'手机','pid'=>0),
    array('id'=>3,'name'=>'笔记本','pid'=>1),
    array('id'=>4,'name'=>'台式机','pid'=>1),
    array('id'=>5,'name'=>'智能机','pid'=>2),
    array('id'=>6,'name'=>'功能机','pid'=>2),
    array('id'=>7,'name'=>'超级本','pid'=>3),
    array('id'=>8,'name'=>'游戏本','pid'=>3),
)
Copy after login

The goal is to convert it into the following structure
Computer— Notebook——-Ultrabook——-Game notebook—Desktop mobile phone—Smartphone—Function machine
If represented by an array, you can add a children key to store its subcategories:

array(
    //1对应id,方便直接读取
    1 => array(
        'id'=>1,
        'name'=>'电脑',
        'pid'=>0,
        children=>array(
            &array(
                'id'=>3,
                'name'=>'笔记本',
                'pid'=>1,
                'children'=>array(
                    //此处省略
                )
            ),
            &array(
                'id'=>4,
                'name'=>'台式机',
                'pid'=>1,
                'children'=>array(
                    //此处省略
                )
            ),
        )
    ),
    //其他分类省略
)
Copy after login

Processing Process:

$tree = array();
//第一步,将分类id作为数组key,并创建children单元
foreach($categories as $category){
    $tree[$category['id']] = $category;
    $tree[$category['id']]['children'] = array();
}
//第二部,利用引用,将每个分类添加到父类children数组中,这样一次遍历即可形成树形结构。
foreach ($tree as $k=>$item) {
    if ($item['pid'] != 0) {
        $tree[$item['pid']]['children'][] = &$tree[$k];
    }
}
print_r($tree);
Copy after login

The printing result is as follows:

Array(
    [1] => Array
        (
            [id] => 1
            [name] => 电脑
            [pid] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [name] => 笔记本
                            [pid] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 7
                                            [name] => 超级本
                                            [pid] => 3
                                            [children] => Array
                                                (
                                                )
                                        )
                                    [1] => Array
                                        (
                                            [id] => 8
                                            [name] => 游戏本
                                            [pid] => 3
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )
                    [1] => Array
                        (
                            [id] => 4
                            [name] => 台式机
                            [pid] => 1
                            [children] => Array
                                (
                                )
                        )
                )
        )
    [2] => Array
        (
            [id] => 2
            [name] => 手机
            [pid] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [name] => 智能机
                            [pid] => 2
                            [children] => Array
                                (
                                )
                        )
                    [1] => Array
                        (
                            [id] => 6
                            [name] => 功能机
                            [pid] => 2
                            [children] => Array
                                (
                                )
                        )
                )
        )
    [3] => Array
        (
            [id] => 3
            [name] => 笔记本
            [pid] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 7
                            [name] => 超级本
                            [pid] => 3
                            [children] => Array
                                (
                                )
                        )
                    [1] => Array
                        (
                            [id] => 8
                            [name] => 游戏本
                            [pid] => 3
                            [children] => Array
                                (
                                )
                        )
                )
        )
    [4] => Array
        (
            [id] => 4
            [name] => 台式机
            [pid] => 1
            [children] => Array
                (
                )
        )
    [5] => Array
        (
            [id] => 5
            [name] => 智能机
            [pid] => 2
            [children] => Array
                (
                )
        )
    [6] => Array
        (
            [id] => 6
            [name] => 功能机
            [pid] => 2
            [children] => Array
                (
                )
        )
    [7] => Array
        (
            [id] => 7
            [name] => 超级本
            [pid] => 3
            [children] => Array
                (
                )
        )
    [8] => Array
        (
            [id] => 8
            [name] => 游戏本
            [pid] => 3
            [children] => Array
                (
                )
        )
)
Copy after login

Advantages: The relationship is clear, and it is simple to modify the superior-subordinate relationship.

Disadvantages: Using PHP processing, if the number of categories is huge, the efficiency will also be reduced.


Extension-------Recursive function

 Recursive function is a type of function that we commonly use. The most basic feature is that the function calls itself, but it must be conditionally judged before calling itself, otherwise it will continue to be called indefinitely. What methods can be used to implement recursive functions? This article lists three basic ways. Understanding it requires a certain amount of basic knowledge, including an understanding of global variables, references, static variables, and an understanding of their scope. Recursive functions are also a good technique for solving infinite levels of classification. If you are interested in infinite classification, please refer to PHP to use recursive functions to achieve infinite classification. I'm used to explaining complex truths in layman's terms. If you really don't understand, please refer to the manual.

 Use references as parameters

Regardless of whether references are parameters or not, we must first understand what a reference is? A reference simply means that two variables with different names point to the same storage address. Originally, each variable had its own storage address, and assignment and deletion went their own way. Okay now, the two variables share a storage address. $a=&$b; . What it actually means is that $a has to share a room with $b regardless of its original storage address. Therefore any change to the stored address value will affect both values.​

Functions originally do their own thing, even if they are functions with the same name. Recursive functions consider taking references as parameters and becoming a bridge to form data sharing between two functions. Although the two functions seem to operate on different addresses, they actually operate on the same memory address.

function test($a=0,&$result=array()){
$a++;
if ($a<10) {
    $result[]=$a;
    test($a,$result);
}
echo $a;
return $result;

}
Copy after login


  上面的例子非常简答,以a<10作为判断条件,条件成立,则把a<10作为判断条件,条件成立,则把a赋给result[];将result[];将result的引用传入函数,会将每一次递归产生的a添加到结果数组a添加到结果数组result。因而本例生成的$result数组是 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 ) 。

本例比较有意思的是echo a的值。相信很多人认为是12345678910吧,其实不然,是1098765432。为什么呢?因为函数还没执行echoa的值。相信很多人认为是12345678910吧,其实不然,是1098765432。为什么呢?因为函数还没执行echoa前就进行了下一次的函数递归。真正执行echo a是当a是当a<10条件不满足的时候,echo a,返回a,返回result,对于上一层而言,执行完递归函数,开始执行本层的echo $a,依次类推。

  利用全局变量

  利用全局变量完成递归函数,请确保你确实理解什么是全局变量。global在函数内申明变量不过是外部变量的同名引用。变量的作用范围仍然在本函数范围内。改变这些变量的值,外部同名变量的值自然也改变了。但一旦用了&,同名变量不再是同名引用。利用全局变量实现递归函数没必要理解到这么深的一层,还保持原有对全局变量的看法就可以顺理成章理解递归函数。

function test($a=0,$result=array()){
    global $result;
    $a++;
    if ($a<10) {
        $result[]=$a;
        test($a,$result);
    }
    return $result;
}
Copy after login


  利用静态变量

  我们常常在类中见到static,今天我们把它利用到递归函数中。请记住static的作用:仅在第一次调用函数的时候对变量进行初始化,并且保留变量值。

例子:

function test(){
static $count=0;
echo $count;

$count++;
}
test();
test();
test();
test();
test();
Copy after login


  请问这一段代码的执行结果是多少?是00000么?必然不是。是01234。首先第一次调用test(),static对 $count 进行初始化,其后每一次执行完都会保留 $count 的值,不再进行初始化,相当于直接忽略了 static$count=0; 这一句。

  因而将static应用到递归函数作用可想而知。在将需要作为递归函数间作为“桥梁"的变量利用static进行初始化,每一次递归都会保留"桥梁变量"的值。

function test($a=0){
    static $result=array();
    $a++;
    if ($a<10) {
        $result[]=$a;
        test($a);
    }
    return $result;
}
Copy after login

  总结

  所谓递归函数,重点是如何处理函数调用自身是如何保证所需要的结果得以在函数间合理"传递",当然也有不需要函数之间传值得递归函数,例如:

function test($a=0){
    $a++;
    if ($a<10) {
        echo $a;

        test($a);
    }
}
Copy after login

面对这样的函数,深入理解变量引用相关知识对解决这类问题大有裨益。

相关文章:

php递归实现无限级分类树

揭露php无限级分类的原理

php无限级分类实现方法分析

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template