Send a limit recursive traversal of the problem for everyone to overcome, first of all, I think of the method is not good, I think the best recursive learning can try

Source: Internet
Author: User
There is such an array
Array ([0] = = Array ([id] = [name] = Sports Department [parent_id] + 46                            [Children] = = Array ([0] = = Array (                            [id] = [name] + Sports 1 professional [parent_id] = 49 [children] = = Array ([0]                                            = = Array ([id] = 57                                        [Name] = 1 class [parent_id] + 52                                            ) [1] = = Array (                               [ID] [[name] = + 2 class             [PARENT_ID] = [2] = Ar                                            Ray ([id] = 59                                        [Name] + Study Abroad class [parent_id] = 52                        ))) [1] = = Array                            ([id] = [name] + Sports 2 Professional                                    [PARENT_ID] = [children] = = Array (                                            [0] = = Array (                                            [ID] [Name] = 1 class   [parent_id] = 53                                     ) [1] = = Array ([id] = [name] + =                                    Class 2 [parent_id] = 53) [2] = = Array ([ ID] = [name] + 3 class [Parent                                        _ID] [3] = = Array                                            ([id] = 63                                        [Name] = 4 class [parent_id] + 53                       )             [4] = = Array ([id] =&gt ;  [Name] + 5 class [parent_id] =        53))) ))


The exact array is like this.

Because this layer is not fixed, there may be children, I would like to set a number, limit its traversal layer, such as limit traversal layer, the result is as follows:
Can be understood as a children is a layer, please master shot
Array (    [0] = = Array        (            [id] = [            name] = Sports Department            [PARENT_ID] + [            children] = Array                (                    [0] = =                        array                            ([id] = [                            name] = + Sport 1 Professional                            [parent_id]                    [1] = = Array                        (                            [id] = +                            [name] = + Sport 2 professional                            [parent_id]        ()))))


Reply to discussion (solution)

Recursive pseudo-code:

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

$ar = Array (0 = = Array (' id ' = ' + ', ' name ' = ' Sports Department ', ' parent_id ' = ' + '), ' children ' = Array (0 = = Array (' id ' = ' ' "'", ' name ' = ' = ' Sports 1 Professional ', ' parent_id ' = ' 49 ')  , ' children ' = = Array (0 = = Array (' id ' = ' + ', ' name ')             = ' 1 ', ' parent_id ' + ', ', ', ' 1 = = array (' id ' = ' 58 ',            ' Name ' = ' 2 ', ' parent_id ' + ', ', ' 2 = = Array (  ' id ' = ' (') ', ' name ' = + ' Study Abroad class ', ' parent_id ' + ' 52 ',), 1  = = Array (' id ' = ' + ', ' name ' = ' + ' Sports 2 professional ', ' parent_id ' + ', ' children '            = = Array (0 = = Array (' id ' = ' + ', ' name ' = ' 1 class '), ' parent_id ' = '",", "1" (' id ' = ' + ', ' name ' = ' 2 class ', ' parent_id ' = ' + ', ' 2 = = array (' id ' = ' + ', ' name ' = +            ' Class 3 ', ' parent_id ' = ' + ', ', ' 3 = = Array (' id ' = ' 63 ', ' Name ' = ' 4 ', ' parent_id ' = ' + ', ' 4 = = array (' ID ') = ' + ', ' name ' = ' 5 class ', ' parent_id ' = ' + ', '), '), '), ', '); func  tion 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));
Array (    [0] = = Array        (            [id] = [            name] = Sports Department            [PARENT_ID] + [            children] = Array                (                    [0] = =                        array                            ([id] = [                            name] = + Sport 1 Professional                            [parent_id]                    [1] = = Array                        (                            [id] = +                            [name] = + Sport 2 professional                            [parent_id]        ()))))

Can optimize

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

The solution was perfect, thank you very much.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.