简体   繁体   中英

Help with recursive function to create parent/child tree from one db query

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

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

  }
}

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