title: Help with recursive function to create parent/child tree from one db query
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>My array produced from the db query contains data similar to this:</p>
  
  <pre><code>Array
  (
      [0] =&gt; stdClass Object
          (
              [role_id] =&gt; 1
              [role_name] =&gt; tester
              [parent_id] =&gt; 0
          )
  
  )
  </code></pre>
  
  <p>I then pass this array to the function below to create the tree.</p>
  
  <pre><code>function create_role_tree($data) {
  
  $roles = array();
  
  foreach ($data as $tkey =&gt; $tval) {
  
      $role = array();
  
      $role['role_id'] = $data[$tkey]-&gt;role_id;
      $role['role_name'] = $data[$tkey]-&gt;role_name;
  
      $children = build_child($data, $data[$tkey]-&gt;role_id);
  
      if( !empty($children) ) {
  
      $role['children'] = $children;
  
      }
  
      $roles[] = $role;
  
      }
  
      return $roles;
  
      }
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>    function build_child($data, $parent) {
  
      $roles = array();
  
      foreach($data as $tkey =&gt; $tval) {
      if($data[$tkey]-&gt;parent_id==$parent) {
  
      $role = array();
  
      $role['role_id'] = $data[$tkey]-&gt;role_id;
      $role['role_name'] = $data[$tkey]-&gt;role_name;
  
      $children = build_child($data, $data[$tkey]-&gt;role_id);
  
      if( !empty($children) ) {
  
      $role['children'] = $children;
  
      }
      $roles[] = $role;
  
      return $roles;
      }
  
      }
      }
  </code></pre>
  
  <p>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?</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [role_id] =&gt; 1
              [role_name] =&gt; tester
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [role_id] =&gt; 4
                              [role_name] =&gt; test 2
                          )
  
                  )
  
          )
  
      [1] =&gt; Array
          (
              [role_id] =&gt; 4
              [role_name] =&gt; test 2
          )
  
      [2] =&gt; Array
          (
              [role_id] =&gt; 5
              [role_name] =&gt; test 3
          )
  
      [3] =&gt; Array
          (
              [role_id] =&gt; 6
              [role_name] =&gt; uyuiy
          )
  
      [4] =&gt; Array
          (
              [role_id] =&gt; 7
              [role_name] =&gt; uyuiy
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [role_id] =&gt; 10
                              [role_name] =&gt; bamm
                              [children] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [role_id] =&gt; 11
                                              [role_name] =&gt; testing tree
                                          )
  
                                  )
  
                          )
  
                  )
  
          )
  
      [5] =&gt; Array
          (
              [role_id] =&gt; 8
              [role_name] =&gt; uyuiy
          )
  
      [6] =&gt; Array
          (
              [role_id] =&gt; 9
              [role_name] =&gt; test new
          )
  
      [7] =&gt; Array
          (
              [role_id] =&gt; 10
              [role_name] =&gt; bamm
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [role_id] =&gt; 11
                              [role_name] =&gt; testing tree
                          )
  
                  )
  
          )
  
      [8] =&gt; Array
          (
              [role_id] =&gt; 11
              [role_name] =&gt; testing tree
          )
  
  )
  </code></pre>
  
  <p>Thanks for looking</p>