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

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

藏色散人
藏色散人forward
2020-01-28 14:59:152866browse

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'],
];

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;
}

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"
  }
]

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;
}

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": []
      }
    ]
  }
]

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete