Home > Backend Development > PHP Tutorial > PHP uses recursion to find data hierarchically (detailed code explanation)

PHP uses recursion to find data hierarchically (detailed code explanation)

藏色散人
Release: 2023-04-08 11:10:01
forward
2925 people have browsed it

PHP uses recursion to find data hierarchically (detailed code explanation)

Today we mainly introduce the use of recursion to find data by level.

The principle is quite simple. It mainly searches for children through a level-by-level loop of parent IDs. It is also easy to implement using PHP loop code. However, if there are more levels, PHP will have more repeated codes. This You can use recursion to achieve this function.

1. First find out the data to be used to form an array (avoid querying the database recursively, and then form the data you need based on this array)

For example, get the following data:

$data = [
    ['id' => '1', 'pid' => '0', 'dsp' => '1'],
    ['id' => '2', 'pid' => '0', 'dsp' => '2'],
    ['id' => '3', 'pid' => '0', 'dsp' => '3'],
    ['id' => '4', 'pid' => '1', 'dsp' => '1-4'],
    ['id' => '5', 'pid' => '4', 'dsp' => '1-4-5'],
    ['id' => '6', 'pid' => '5', 'dsp' => '1-4-5-6'],
    ['id' => '7', 'pid' => '3', 'dsp' => '3-7'],
    ['id' => '8', 'pid' => '2', 'dsp' => '2-8'],
    ['id' => '9', 'pid' => '1', 'dsp' => '1-9'],
    ['id' => '10', 'pid' => '4', 'dsp' => '1-4-10'],
];
Copy after login

2. Next, use recursion to reorganize the data so that the data is displayed hierarchically.

/**
 * 根据父级id查找子级数据
 * @param $data     要查询的数据
 * @param int $pid 父级id
 */
public function recursion($data, $pid = 0)
{
    static $child = [];   // 定义存储子级数据数组
    foreach ($data as $key => $value) {
        if ($value['pid'] == $pid) {
            $child[] = $value;   // 满足条件的数据添加进child数组
            unset($data[$key]);  // 使用过后可以销毁
            $this->recursion($data, $value['id']);   // 递归调用,查找当前数据的子级
        }
    }
    return $child;
}
Copy after login

Get the result:

[
  {
    "id": "1",
    "pid": "0",
    "dsp": "1"
  },
  {
    "id": "4",
    "pid": "1",
    "dsp": "1-4"
  },
  {
    "id": "5",
    "pid": "4",
    "dsp": "1-4-5"
  },
  {
    "id": "6",
    "pid": "5",
    "dsp": "1-4-5-6"
  },
  {
    "id": "10",
    "pid": "4",
    "dsp": "1-4-10"
  },
  {
    "id": "9",
    "pid": "1",
    "dsp": "1-9"
  },
  {
    "id": "2",
    "pid": "0",
    "dsp": "2"
  },
  {
    "id": "8",
    "pid": "2",
    "dsp": "2-8"
  },
  {
    "id": "3",
    "pid": "0",
    "dsp": "3"
  },
  {
    "id": "7",
    "pid": "3",
    "dsp": "3-7"
  }
]
Copy after login

3. You can also use the following method to display a more layered look.

/**
 * 根据父级id查找子级数据
 * @param $data     要查询的数据
 * @param int $pid 父级id
 */
public function recursion($data, $pid = 0)
{
    $child = [];   // 定义存储子级数据数组
    foreach ($data as $key => $value) {
        if ($value['pid'] == $pid) {
            unset($data[$key]);  // 使用过后可以销毁
            $value['child'] = $this->recursion($data, $value['id']);   // 递归调用,查找当前数据的子级
            $child[] = $value;   // 把子级数据添加进数组
        }
    }
    return $child;
}
Copy after login

Get the result:

[
  {
    "id": "1",
    "pid": "0",
    "dsp": "1",
    "child": [
      {
        "id": "4",
        "pid": "1",
        "dsp": "1-4",
        "child": [
          {
            "id": "5",
            "pid": "4",
            "dsp": "1-4-5",
            "child": [
              {
                "id": "6",
                "pid": "5",
                "dsp": "1-4-5-6",
                "child": []
              }
            ]
          },
          {
            "id": "10",
            "pid": "4",
            "dsp": "1-4-10",
            "child": []
          }
        ]
      },
      {
        "id": "9",
        "pid": "1",
        "dsp": "1-9",
        "child": []
      }
    ]
  },
  {
    "id": "2",
    "pid": "0",
    "dsp": "2",
    "child": [
      {
        "id": "8",
        "pid": "2",
        "dsp": "2-8",
        "child": []
      }
    ]
  },
  {
    "id": "3",
    "pid": "0",
    "dsp": "3",
    "child": [
      {
        "id": "7",
        "pid": "3",
        "dsp": "3-7",
        "child": []
      }
    ]
  }
]
Copy after login

For more related php knowledge, please visit php tutorial!

The above is the detailed content of PHP uses recursion to find data hierarchically (detailed code explanation). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
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