欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

发个限制递归遍历次数的问题给大家解困,首先声明,我想到的方法不好,觉得递归学的好的可以试试

程序员文章站 2022-06-17 20:53:23
...
有这么一个数组
Array(    [0] => Array        (            [id] => 49            [name] => 体育部            [parent_id] => 46            [children] => Array                (                    [0] => Array                        (                            [id] => 52                            [name] => 体育1专业                            [parent_id] => 49                            [children] => Array                                (                                    [0] => Array                                        (                                            [id] => 57                                            [name] => 1班                                            [parent_id] => 52                                        )                                    [1] => Array                                        (                                            [id] => 58                                            [name] => 2班                                            [parent_id] => 52                                        )                                    [2] => Array                                        (                                            [id] => 59                                            [name] => 外留学班                                            [parent_id] => 52                                        )                                )                        )                    [1] => Array                        (                            [id] => 53                            [name] => 体育2专业                            [parent_id] => 49                            [children] => Array                                (                                    [0] => Array                                        (                                            [id] => 60                                            [name] => 1班                                            [parent_id] => 53                                        )                                    [1] => Array                                        (                                            [id] => 61                                            [name] => 2班                                            [parent_id] => 53                                        )                                    [2] => Array                                        (                                            [id] => 62                                            [name] => 3班                                            [parent_id] => 53                                        )                                    [3] => Array                                        (                                            [id] => 63                                            [name] => 4班                                            [parent_id] => 53                                        )                                    [4] => Array                                        (                                            [id] => 64                                            [name] => 5班                                            [parent_id] => 53                                        )                                )                        )                )        ))


具体的数组是这样的

因为这个层数是不固定的,下面可能还有children,我想设置一个数,限制它的遍历层数,例如限制遍历一层,结果如下:
可以理解成一个 children 是一层,请各位高手出手
Array(    [0] => Array        (            [id] => 49            [name] => 体育部            [parent_id] => 46            [children] => Array                (                    [0] => Array                        (                            [id] => 52                            [name] => 体育1专业                            [parent_id] => 49                        )                    [1] => Array                        (                            [id] => 53                            [name] => 体育2专业                            [parent_id] => 49                        )                )        ))


回复讨论(解决方案)

递归的伪代码:

function getTortoise($arr, $flag){    if($flag===0) return;    foreach ($arr as $k => $v) {        //do something...        getTortoise($arr,$flag--);    }}

$ar = array (  0 =>   array (    'id' => '49',    'name' => '体育部',    'parent_id' => '46',    'children' =>     array (      0 =>       array (        'id' => '52',        'name' => '体育1专业',        'parent_id' => '49',        'children' =>         array (          0 =>           array (            'id' => '57',            'name' => '1班',            'parent_id' => '52',          ),          1 =>           array (            'id' => '58',            'name' => '2班',            'parent_id' => '52',          ),          2 =>           array (            'id' => '59',            'name' => '外留学班',            'parent_id' => '52',          ),        ),      ),      1 =>       array (        'id' => '53',        'name' => '体育2专业',        'parent_id' => '49',        'children' =>         array (          0 =>           array (            'id' => '60',            'name' => '1班',            'parent_id' => '53',          ),          1 =>           array (            'id' => '61',            'name' => '2班',            'parent_id' => '53',          ),          2 =>           array (            'id' => '62',            'name' => '3班',            'parent_id' => '53',          ),          3 =>           array (            'id' => '63',            'name' => '4班',            'parent_id' => '53',          ),          4 =>           array (            'id' => '64',            'name' => '5班',            'parent_id' => '53',          ),        ),      ),    ),  ),);function foo($ar, $deep=-1) {  if($deep $r) {    if(isset($r['children'])) {      $t = $r['children'];      unset($r['children']);      if($deep) $r['children'] = foo($t, $deep-1);    }    $res[$k] = $r;  }  return $res;}print_r(foo($ar, 1));
Array(    [0] => Array        (            [id] => 49            [name] => 体育部            [parent_id] => 46            [children] => Array                (                    [0] => Array                        (                            [id] => 52                            [name] => 体育1专业                            [parent_id] => 49                        )                    [1] => Array                        (                            [id] => 53                            [name] => 体育2专业                            [parent_id] => 49                        )                )        ))

可以优化一下

function foo($ar, $deep=-1) {  if($deep $r) {    if(isset($r['children'])) {      if($deep) $r['children'] = foo($r['children'], $deep-1);      else unset($r['children']);    }    $res[$k] = $r;  }  return $res;}

解决得很完美,非常感谢。