简体   繁体   中英

convert nested foreach tree into recursive php function

Need to make recursive function from the below code. tried multiple ways but failed. because I don't know how much depth will the tree have. depth may be varying according to the data.

$tot_terms = [];
foreach ($tree as $key => $val) {
      $tot_terms[$val->depth][] = $val->tid;
      if (!empty($val->children)) {
        foreach ($val->children as $key1 => $val1) {
          $tot_terms[$val1->depth][] = $val1->tid;
          if ($val1->children) {
            foreach ($val1->children as $key2 => $val2) {
              $tot_terms[$val2->depth][] = $val2->tid;
              if ($val2->children) {
                foreach ($val2->children as $key3 => $val3) {
                  $tot_terms[$val3->depth][] = $val3->tid;
                  if ($val3->children) {
                    foreach ($val3->children as $key4 => $val4) {
                      $tot_terms[$val4->depth][] = $val4->tid;
                      if ($val4->children) {
                        foreach ($val4->children as $key5 => $val5) {
                          $tot_terms[$val5->depth][] = $val5->tid;
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

and the result should be like this

Array
(
    [0] => Array
        (
            [0] => 20011
            [1] => 19991
            [2] => 20000
        )
    [1] => Array
        (
            [0] => 20010
            [1] => 19995
            [2] => 19994
            [3] => 19990
            [4] => 20005
            [5] => 19985
            [6] => 19999
            [7] => 19998
        )
    [2] => Array
        (
            [0] => 20012
            [1] => 20006
            [2] => 19996
            [3] => 19993
            [4] => 19989
            [5] => 19988
            [6] => 20004
            [7] => 19984
            [8] => 20001
        )
    [3] => Array
        (
            [0] => 20007
            [1] => 20009
            [2] => 20008
            [3] => 19992
            [4] => 19987
            [5] => 19986
            [6] => 19983
            [7] => 19982
            [8] => 20003
            [9] => 20002
        )
    [4] => Array
        (
            [0] => 19997
        )
    [5] => Array
        (
            [0] => 19981
        )
)

Tried according to other post but it is failing. input $tree is something that is array and an object. will post in next comment if required as total body count is more than its expected

We can use a handler function to store data. Note that we are passing the array by reference.

function func( $item, &$data ) {
    if (  is_empty( $item ) ) return;
    if (  ! is_object( $item ) ) return;

    foreach ($item as $key => $val) {
      $data[$val->depth][] = $val->tid;
      func( $val->children, $data );
    }
}

function func_handler( $item ) {
    $data = [];
    func( $item, $data );
    return $data;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM