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

时间:2011-03-12 17:10:50

标签: php loops hierarchical-data

我一直在研究一个函数,从一个db查询生成一个无限制的父/子关系的多维数组。我非常接近完成这个,但我仍然有一些问题。

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

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

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
        )

)

感谢您寻找

1 个答案:

答案 0 :(得分:1)

您只需要跳过不属于主数组的元素,即带有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...

  }
}