当前位置:首页 > PHP教程 > php应用 > 列表

PHP使用递归按层级查找数据(代码详解)

发布:smiling 来源: PHP粉丝网  添加日期:2022-05-30 08:47:15 浏览: 评论:0 

今天主要介绍一下使用递归来按层级查找数据。

原理挺简单的,主要是通过父级id一级一级的循环查找子级,使用PHP循环代码也很容易实现,不过如果层级越多,PHP重复代码也越多,这时可以使用递归来实现这功能。

1、首先查出要使用的数据组成一个数组(避免递归里查询数据库,之后根据这个数组组成自己需要的数据就可以了)

比如得到如下数据:

  1. $data = [ 
  2.  
  3.     ['id' => '1', 'pid' => '0', 'dsp' => '1'], 
  4.  
  5.     ['id' => '2', 'pid' => '0', 'dsp' => '2'], 
  6.  
  7.     ['id' => '3', 'pid' => '0', 'dsp' => '3'], 
  8.  
  9.     ['id' => '4', 'pid' => '1', 'dsp' => '1-4'], 
  10.  
  11.     ['id' => '5', 'pid' => '4', 'dsp' => '1-4-5'], 
  12.  
  13.     ['id' => '6', 'pid' => '5', 'dsp' => '1-4-5-6'], 
  14.  
  15.     ['id' => '7', 'pid' => '3', 'dsp' => '3-7'], 
  16.  
  17.     ['id' => '8', 'pid' => '2', 'dsp' => '2-8'], 
  18.  
  19.     ['id' => '9', 'pid' => '1', 'dsp' => '1-9'], 
  20.  
  21.     ['id' => '10', 'pid' => '4', 'dsp' => '1-4-10'], 
  22.  
  23. ]; 

2、接下来使用递归重组数据,使数据按层级显示。

  1. /** 
  2.  
  3.  * 根据父级id查找子级数据 
  4.  
  5.  * @param $data     要查询的数据 
  6.  
  7.  * @param int $pid 父级id 
  8.  
  9.  */ 
  10.  
  11. public function recursion($data, $pid = 0) 
  12.  
  13. { 
  14.  
  15.     static $child = [];   // 定义存储子级数据数组 
  16.  
  17.     foreach ($data as $key => $value) { 
  18.  
  19.         if ($value['pid'] == $pid) { 
  20.  
  21.             $child[] = $value;   // 满足条件的数据添加进child数组 
  22.  
  23.             unset($data[$key]);  // 使用过后可以销毁 
  24.  
  25.             $this->recursion($data, $value['id']);   // 递归调用,查找当前数据的子级 
  26.  
  27.         } 
  28.  
  29.     } 
  30.  
  31.     return $child; 
  32.  
  33. } 

得到结果:

  1. [ 
  2.  
  3.   { 
  4.  
  5.     "id": "1", 
  6.  
  7.     "pid": "0", 
  8.  
  9.     "dsp": "1" 
  10.  
  11.   }, 
  12.  
  13.   { 
  14.  
  15.     "id": "4", 
  16.  
  17.     "pid": "1", 
  18.  
  19.     "dsp": "1-4" 
  20.  
  21.   }, 
  22.  
  23.   { 
  24.  
  25.     "id": "5", 
  26.  
  27.     "pid": "4", 
  28.  
  29.     "dsp": "1-4-5" 
  30.  
  31.   }, 
  32.  
  33.   { 
  34.  
  35.     "id": "6", 
  36.  
  37.     "pid": "5", 
  38.  
  39.     "dsp": "1-4-5-6" 
  40.  
  41.   }, 
  42.  
  43.   { 
  44.  
  45.     "id": "10", 
  46.  
  47.     "pid": "4", 
  48.  
  49.     "dsp": "1-4-10" 
  50.  
  51.   }, 
  52.  
  53.   { 
  54.  
  55.     "id": "9", 
  56.  
  57.     "pid": "1", 
  58.  
  59.     "dsp": "1-9" 
  60.  
  61.   }, 
  62.  
  63.   { 
  64.  
  65.     "id": "2", 
  66.  
  67.     "pid": "0", 
  68.  
  69.     "dsp": "2" 
  70.  
  71.   }, 
  72.  
  73.   { 
  74.  
  75.     "id": "8", 
  76.  
  77.     "pid": "2", 
  78.  
  79.     "dsp": "2-8" 
  80.  
  81.   }, 
  82.  
  83.   { 
  84.  
  85.     "id": "3", 
  86.  
  87.     "pid": "0", 
  88.  
  89.     "dsp": "3" 
  90.  
  91.   }, 
  92.  
  93.   { 
  94.  
  95.     "id": "7", 
  96.  
  97.     "pid": "3", 
  98.  
  99.     "dsp": "3-7" 
  100.  
  101.   } 
  102.  
  103. ] 

3、还可以使用下面的方法,显示更有层次感。

  1. /** 
  2.  
  3.  * 根据父级id查找子级数据 
  4.  
  5.  * @param $data     要查询的数据 
  6.  
  7.  * @param int $pid 父级id 
  8.  
  9.  */ 
  10.  
  11. public function recursion($data, $pid = 0) 
  12.  
  13. { 
  14.  
  15.     $child = [];   // 定义存储子级数据数组 
  16.  
  17.     foreach ($data as $key => $value) { 
  18.  
  19.         if ($value['pid'] == $pid) { 
  20.  
  21.             unset($data[$key]);  // 使用过后可以销毁 
  22.  
  23.             $value['child'] = $this->recursion($data, $value['id']);   // 递归调用,查找当前数据的子级 
  24.  
  25.             $child[] = $value;   // 把子级数据添加进数组 
  26.  
  27.         } 
  28.  
  29.     } 
  30.  
  31.     return $child; 
  32.  
  33. } 

得到结果:

  1. [ 
  2.  
  3.   { 
  4.  
  5.     "id": "1", 
  6.  
  7.     "pid": "0", 
  8.  
  9.     "dsp": "1", 
  10.  
  11.     "child": [ 
  12.  
  13.       { 
  14.  
  15.         "id": "4", 
  16.  
  17.         "pid": "1", 
  18.  
  19.         "dsp": "1-4", 
  20.  
  21.         "child": [ 
  22.  
  23.           { 
  24.  
  25.             "id": "5", 
  26.  
  27.             "pid": "4", 
  28.  
  29.             "dsp": "1-4-5", 
  30.  
  31.             "child": [ 
  32.  
  33.               { 
  34.  
  35.                 "id": "6", 
  36.  
  37.                 "pid": "5", 
  38.  
  39.                 "dsp": "1-4-5-6", 
  40.  
  41.                 "child": [] 
  42.  
  43.               } 
  44.  
  45.             ] 
  46.  
  47.           }, 
  48.  
  49.           { 
  50.  
  51.             "id": "10", 
  52.  
  53.             "pid": "4", 
  54.  
  55.             "dsp": "1-4-10", 
  56.  
  57.             "child": [] 
  58.  
  59.           } 
  60.  
  61.         ] 
  62.  
  63.       }, 
  64.  
  65.       { 
  66.  
  67.         "id": "9", 
  68.  
  69.         "pid": "1", 
  70.  
  71.         "dsp": "1-9", 
  72.  
  73.         "child": [] 
  74.  
  75.       } 
  76.  
  77.     ] 
  78.  
  79.   }, 
  80.  
  81.   { 
  82.  
  83.     "id": "2", 
  84.  
  85.     "pid": "0", 
  86.  
  87.     "dsp": "2", 
  88.  
  89.     "child": [ 
  90.  
  91.       { 
  92.  
  93.         "id": "8", 
  94.  
  95.         "pid": "2", 
  96.  
  97.         "dsp": "2-8", 
  98.  
  99.         "child": [] 
  100.  
  101.       } 
  102.  
  103.     ] 
  104.  
  105.   }, 
  106.  
  107.   { 
  108.  
  109.     "id": "3", 
  110.  
  111.     "pid": "0", 
  112.  
  113.     "dsp": "3", 
  114.  
  115.     "child": [ 
  116.  
  117.       { 
  118.  
  119.         "id": "7", 
  120.  
  121.         "pid": "3", 
  122.  
  123.         "dsp": "3-7", 
  124.  
  125.         "child": [] 
  126.  
  127.       } 
  128.  
  129.     ] 
  130.  
  131.   } 
  132.  
  133. ]

Tags: PHP递归按层级查找数据

分享到: