Home  >  Article  >  Backend Development  >  Recursive implementation of infinite classification in PHP

Recursive implementation of infinite classification in PHP

王林
王林Original
2023-05-07 11:32:072519browse

In web development, classification is a very common function. And the infinite classification, as the name suggests, is to classify according to the infinite level. In the interaction process between the front and back ends, certain methods must be used to achieve this classification, and PHP's recursion is one of them.

1. What is recursion

Recursion refers to a technique of calling itself during the running of the program. Through recursion, complex problems can be simplified into simple ones, thereby better solving the problem. To use recursion in PHP, you need to clarify two elements of recursion: the recursive end condition and the recursive expression.

The recursion end condition refers to the judgment condition for when the recursion stops. This usually refers to reaching a certain depth, state or having a certain value. During the recursive process, if there is no end condition, the program will fall into an infinite loop.

Recursive expression refers to continuing to perform recursive operations during the recursive process. Normally, recursive expressions are implemented inside recursive functions by calling functions themselves.

2. How to use recursion to achieve infinite classification

We can use recursion to achieve infinite classification. Specifically, we can define a recursive function that receives a parameter, which is an array representing a classification list at a certain level. Recursive operations are performed inside the function, and each category is traversed in turn. If this category has subcategories, the subcategories are traversed through the recursive function. At the end of the traversal, the entire categorical array is returned.

The specific implementation is as follows:

function infiniteCategory($data, $pid = 0, $level = 0)
{
    static $result = array();
    foreach ($data as $key => $value) {
        if ($value['parent_id'] == $pid) {
            $value['level'] = $level;
            $result[] = $value;
            infiniteCategory($data, $value['id'], $level + 1);
        }
    }
    return $result;
}

The analysis is as follows:

(1) This function receives three parameters, one is the classification list array $data, and the other is the parent id default $pid is 0, and one is $level, which defaults to 0.

(2) Define a static variable to store the final classification result.

(3) Traverse the category list through a foreach loop. If the parent_id of a category is equal to $pid, it means that this category belongs to the current level and is added to the final result array $result.

(4) Then call the recursive function, passing in the id of the current category as $pid, and $level 1 as the level of the next level of classification.

(5) After the traversal is completed, return the result array.

Let’s test it:

$data = array(
    array('id' => 1, 'name' => '电脑', 'parent_id' => 0),
    array('id' => 2, 'name' => '手机', 'parent_id' => 0),
    array('id' => 3, 'name' => '笔记本', 'parent_id' => 1),
    array('id' => 4, 'name' => '台式机', 'parent_id' => 1),
    array('id' => 5, 'name' => '智能手机', 'parent_id' => 2),
    array('id' => 6, 'name' => '非智能手机', 'parent_id' => 2),
    array('id' => 7, 'name' => '微型笔记本', 'parent_id' => 3),
    array('id' => 8, 'name' => '超级台式机', 'parent_id' => 4),
    array('id' => 9, 'name' => '小型台式机', 'parent_id' => 4),
);

var_dump(infiniteCategory($data));

The running results are as follows:

array(9) {
  [0]=>
  array(4) {
    ["id"]=>
    int(1)
    ["name"]=>
    string(6) "电脑"
    ["parent_id"]=>
    int(0)
    ["level"]=>
    int(0)
  }
  [1]=>
  array(4) {
    ["id"]=>
    int(3)
    ["name"]=>
    string(9) "笔记本"
    ["parent_id"]=>
    int(1)
    ["level"]=>
    int(1)
  }
  [2]=>
  array(4) {
    ["id"]=>
    int(7)
    ["name"]=>
    string(15) "微型笔记本"
    ["parent_id"]=>
    int(3)
    ["level"]=>
    int(2)
  }
  [3]=>
  array(4) {
    ["id"]=>
    int(4)
    ["name"]=>
    string(9) "台式机"
    ["parent_id"]=>
    int(1)
    ["level"]=>
    int(1)
  }
  [4]=>
  array(4) {
    ["id"]=>
    int(9)
    ["name"]=>
    string(18) "小型台式机"
    ["parent_id"]=>
    int(4)
    ["level"]=>
    int(2)
  }
  [5]=>
  array(4) {
    ["id"]=>
    int(8)
    ["name"]=>
    string(18) "超级台式机"
    ["parent_id"]=>
    int(4)
    ["level"]=>
    int(2)
  }
  [6]=>
  array(4) {
    ["id"]=>
    int(2)
    ["name"]=>
    string(6) "手机"
    ["parent_id"]=>
    int(0)
    ["level"]=>
    int(0)
  }
  [7]=>
  array(4) {
    ["id"]=>
    int(5)
    ["name"]=>
    string(12) "智能手机"
    ["parent_id"]=>
    int(2)
    ["level"]=>
    int(1)
  }
  [8]=>
  array(4) {
    ["id"]=>
    int(6)
    ["name"]=>
    string(15) "非智能手机"
    ["parent_id"]=>
    int(2)
    ["level"]=>
    int(1)
  }
}

As you can see, the classification list has been successfully converted into Infinitus classification, according to the value of the level field , you can see the level at which each category is located.

3. Summary

The method of realizing Infinitus classification through recursion allows us to quickly complete the classification function in web development and has high flexibility. When using recursion, you need to pay attention to the setting of the end condition, otherwise you may fall into an infinite loop.

The above is the detailed content of Recursive implementation of infinite classification in PHP. 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