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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-20 12:30:34
Original
880 people have browsed it

有这么一个数组

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                                        )                                )                        )                )        ))
Copy after login


具体的数组是这样的

因为这个层数是不固定的,下面可能还有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                        )                )        ))
Copy after login
Copy after login


回复讨论(解决方案)

递归的伪代码:

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

$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 < 0) return $ar;  $res = array();  foreach($ar as $k=>$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));
Copy after login
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                        )                )        ))
Copy after login
Copy after login

可以优化一下

function foo($ar, $deep=-1) {  if($deep < 0) return $ar;  $res = array();  foreach($ar as $k=>$r) {    if(isset($r['children'])) {      if($deep) $r['children'] = foo($r['children'], $deep-1);      else unset($r['children']);    }    $res[$k] = $r;  }  return $res;}
Copy after login

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

source:php.cn
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