帮助递归函数从一个数据库查询创建父/子树

发布于 2024-10-21 11:34:13 字数 3694 浏览 0 评论 0原文

我已经研究一个函数一段时间了,它可以从一个数据库查询生成无限父/子关系的多维数组。我已经非常接近完成这个任务了,但是我仍然有一些问题。

我将复制下面的代码并对其进行评论以显示问题。如果可能的话,我希望这里有人能帮助我。

我从数据库查询生成的数组包含与此类似的数据:

Array
(
    [0] => stdClass Object
        (
            [role_id] => 1
            [role_name] => tester
            [parent_id] => 0
        )

)

然后我将此数组传递给下面的函数以创建树。

function create_role_tree($data) {

$roles = array();

foreach ($data as $tkey => $tval) {

    $role = array();

    $role['role_id'] = $data[$tkey]->role_id;
    $role['role_name'] = $data[$tkey]->role_name;

    $children = build_child($data, $data[$tkey]->role_id);

    if( !empty($children) ) {

    $role['children'] = $children;

    }

    $roles[] = $role;

    }

    return $roles;

    }

我添加了如下所示的另一个函数,因为我遇到了创建无限循环的问题。但最终,如果可能的话,我希望从一个函数生成这个数组。

    function build_child($data, $parent) {

    $roles = array();

    foreach($data as $tkey => $tval) {
    if($data[$tkey]->parent_id==$parent) {

    $role = array();

    $role['role_id'] = $data[$tkey]->role_id;
    $role['role_name'] = $data[$tkey]->role_name;

    $children = build_child($data, $data[$tkey]->role_id);

    if( !empty($children) ) {

    $role['children'] = $children;

    }
    $roles[] = $role;

    return $roles;
    }

    }
    }

下面是运行上述两个函数后剩下的数组。您会看到它几乎产生了正确的结果,但是我遇到的问题是,如果一个角色是另一个角色的子级,它仍然显示在主数组中,我想阻止这种情况发生。有人能帮我阻止这种情况发生吗?

Array
(
    [0] => Array
        (
            [role_id] => 1
            [role_name] => tester
            [children] => Array
                (
                    [0] => Array
                        (
                            [role_id] => 4
                            [role_name] => test 2
                        )

                )

        )

    [1] => Array
        (
            [role_id] => 4
            [role_name] => test 2
        )

    [2] => Array
        (
            [role_id] => 5
            [role_name] => test 3
        )

    [3] => Array
        (
            [role_id] => 6
            [role_name] => uyuiy
        )

    [4] => Array
        (
            [role_id] => 7
            [role_name] => uyuiy
            [children] => Array
                (
                    [0] => Array
                        (
                            [role_id] => 10
                            [role_name] => bamm
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [role_id] => 11
                                            [role_name] => testing tree
                                        )

                                )

                        )

                )

        )

    [5] => Array
        (
            [role_id] => 8
            [role_name] => uyuiy
        )

    [6] => Array
        (
            [role_id] => 9
            [role_name] => test new
        )

    [7] => Array
        (
            [role_id] => 10
            [role_name] => bamm
            [children] => Array
                (
                    [0] => Array
                        (
                            [role_id] => 11
                            [role_name] => testing tree
                        )

                )

        )

    [8] => Array
        (
            [role_id] => 11
            [role_name] => testing tree
        )

)

感谢您的关注

I have been working on a function for a while to produce a multidimensional array of unlimited parent/child relationships from one db query. I am very close to completing this however I still have a few problems.

I will copy my code below and comment on it to show the problems. I'm hoping someone here can help me out if possible.

My array produced from the db query contains data similar to this:

Array
(
    [0] => stdClass Object
        (
            [role_id] => 1
            [role_name] => tester
            [parent_id] => 0
        )

)

I then pass this array to the function below to create the tree.

function create_role_tree($data) {

$roles = array();

foreach ($data as $tkey => $tval) {

    $role = array();

    $role['role_id'] = $data[$tkey]->role_id;
    $role['role_name'] = $data[$tkey]->role_name;

    $children = build_child($data, $data[$tkey]->role_id);

    if( !empty($children) ) {

    $role['children'] = $children;

    }

    $roles[] = $role;

    }

    return $roles;

    }

I added another function shown below because I was having problems that created an infinite loop. But ultimately I would like to get this array produced from one function if possible.

    function build_child($data, $parent) {

    $roles = array();

    foreach($data as $tkey => $tval) {
    if($data[$tkey]->parent_id==$parent) {

    $role = array();

    $role['role_id'] = $data[$tkey]->role_id;
    $role['role_name'] = $data[$tkey]->role_name;

    $children = build_child($data, $data[$tkey]->role_id);

    if( !empty($children) ) {

    $role['children'] = $children;

    }
    $roles[] = $role;

    return $roles;
    }

    }
    }

Below is the array I am left with after running the above two functions. You will see it is very nearly producing the correct results however the problem I am having is that if a role is a child of another role it is still shown in the main array and I want to stop this happening. Could anybody help me prevent this happening?

Array
(
    [0] => Array
        (
            [role_id] => 1
            [role_name] => tester
            [children] => Array
                (
                    [0] => Array
                        (
                            [role_id] => 4
                            [role_name] => test 2
                        )

                )

        )

    [1] => Array
        (
            [role_id] => 4
            [role_name] => test 2
        )

    [2] => Array
        (
            [role_id] => 5
            [role_name] => test 3
        )

    [3] => Array
        (
            [role_id] => 6
            [role_name] => uyuiy
        )

    [4] => Array
        (
            [role_id] => 7
            [role_name] => uyuiy
            [children] => Array
                (
                    [0] => Array
                        (
                            [role_id] => 10
                            [role_name] => bamm
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [role_id] => 11
                                            [role_name] => testing tree
                                        )

                                )

                        )

                )

        )

    [5] => Array
        (
            [role_id] => 8
            [role_name] => uyuiy
        )

    [6] => Array
        (
            [role_id] => 9
            [role_name] => test new
        )

    [7] => Array
        (
            [role_id] => 10
            [role_name] => bamm
            [children] => Array
                (
                    [0] => Array
                        (
                            [role_id] => 11
                            [role_name] => testing tree
                        )

                )

        )

    [8] => Array
        (
            [role_id] => 11
            [role_name] => testing tree
        )

)

Thanks for looking

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

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

发布评论

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

评论(1

当爱已成负担 2024-10-28 11:34:13

您只需跳过不属于主数组的元素,即带有 parent_id != 0 的元素,它们是应该出现在树中其他位置的子元素。

function create_role_tree($data) {
  $roles = array();

  foreach ($data as $tkey => $tval) {

    // Skip element, if it is a child element
    if ($data[$tkey]->parent_id != 0) {
      // Skip to next element
      continue;
    }

    // Else, go on as before...

  }
}

You simple need to skip elements that doesn't belong in the main array, that is, elements with parent_id != 0, which are child elements that should appear somewhere else in the tree.

function create_role_tree($data) {
  $roles = array();

  foreach ($data as $tkey => $tval) {

    // Skip element, if it is a child element
    if ($data[$tkey]->parent_id != 0) {
      // Skip to next element
      continue;
    }

    // Else, go on as before...

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