将嵌套的foreach树转换为递归PHP功能

发布于 2025-02-12 03:57:36 字数 2486 浏览 0 评论 0原文

需要从以下代码中发挥递归功能。尝试了多种方法,但失败了。因为我不知道这棵树会有多少深度。深度可能会根据数据而变化。

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

结果应该是

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
        )
)

根据其他帖子尝试的,但失败了。输入$树是数组和对象。如果需要,将在下一个评论中发布,因为总体数量超过了预期

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2025-02-19 03:57:36

我们可以使用处理程序功能存储数据。请注意,我们正在通过参考传递数组。

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文