I am sorry for the problem of limiting the number of recursive traversal times. First of all, I declare that the method I think is not good. if you think recursion is good, try it.

Source: Internet
Author: User
I am sorry for the problem of limiting the number of recursive traversal times. First of all, I declare that the method I think is not good. if you think recursion is good, try to have such an array.
Array ([0] => Array ([id] => 49 [name] => Sports Department [parent_id] => 46 [children] => Array ([0] => Array ([id] => 52 [name] => Sports 1 Major [parent_id] => 49 [children] => Array ([0] => Array ([id] => 57 [name] => Class 1 [parent_id] => 52) [1] => Array ([id] => 58 [name] => Class 2 [parent_id] => 52) [2] => Array ([id] => 59 [name] => Study abroad class [parent_id] => 52 ))) [1] => Array ([id] => 53 [name] => Sports 2 Major [parent_id] => 49 [children] => Array ([0] => Array ([id] => 60 [name] => Class 1 [parent_id] => 53) [1] => Array ([id] => 61 [name] => Class 2 [parent_id] => 53) [2] => Array ([id] => 62 [name] => Class 3 [parent_id] => 53) [3] => Array ([id] => 63 [name] => Class 4 [parent_id] => 53) [4] => Array ([id] => 64 [name] => Class 5 [parent_id] => 53 ))))))


The specific array is like this.

Because the number of layers is not fixed, there may be children below. I want to set a number to limit the number of traversal layers. for example, to limit the number of traversal layers, the result is as follows:
It can be understood that a children program is a layer-1 program.
Array ([0] => Array ([id] => 49 [name] => Sports Department [parent_id] => 46 [children] => Array ([0] => Array ([id] => 52 [name] => Sports 1 Major [parent_id] => 49) [1] => Array ([id] => 53 [name] => Sports 2 Major [parent_id] => 49 ))))


Reply to discussion (solution)

Recursive pseudocode:

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' => 'Sports department ', 'parent _ id' => '46 ', 'Children '=> array (0 => array ('id' => '52', 'name' => 'Sports 1 professional ', 'parent _ id' => '49', 'Children '=> array (0 => array ('id' => '57 ', 'name' => 'class 1 ', 'parent _ id' => '52',), 1 => array ('id' => '58 ', 'name' => '2', 'parent _ id' => '52 ',), 2 => array ('id' => '59 ', 'name' => 'study abroad ', 'parent _ id' => '52 ',),),), 1 => array ('id' => '53', 'name' => 'Sports 2 professional', 'parent _ id' => '49 ', 'Children '=> array (0 => array ('id' => '60', 'name' => 'class 1 ', 'parent _ id' => '53',), 1 => array ('id' => '61 ', 'name' => '2 ', 'parent _ id' => '53',), 2 => array ('id' => '62', 'name' => '3ban ', 'parent _ id' => '53',), 3 => array ('id' => '63', 'name' => '4ban ', 'parent _ id' => '53',), 4 => array ('id' => '64', 'name' => '5ban ', '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 ));
Array ([0] => Array ([id] => 49 [name] => Sports Department [parent_id] => 46 [children] => Array ([0] => Array ([id] => 52 [name] => Sports 1 Major [parent_id] => 49) [1] => Array ([id] => 53 [name] => Sports 2 Major [parent_id] => 49 ))))

You can optimize it.

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 is 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.