title: PHP reversing parents and children in array tree
body: <p>I've an array/tree with (endless) "parents" instead of children. It looks like this:</p>
  
  <pre><code>array(5) {
    [0]=&gt;
    array(3) {
      ["group_id"]=&gt;
      int(1)
      ["name"]=&gt;
      string(11) "Toplevel"
      ["parents"]=&gt;
      NULL
    }
    [1]=&gt;
    array(3) {
      ["group_id"]=&gt;
      int(2)
      ["name"]=&gt;
      string(13) "SecondLevel"
      ["parents"]=&gt;
      array(1) {
        [0]=&gt;
        array(3) {
          ["group_id"]=&gt;
          int(1)
          ["name"]=&gt;
          string(11) "Toplevel"
          ["parents"]=&gt;
          NULL
        }
      }
    }
    [2]=&gt;
    array(3) {
      ["group_id"]=&gt;
      int(4)
      ["name"]=&gt;
      string(12) "ThirdLevel"
      ["parents"]=&gt;
      array(2) {
        [0]=&gt;
        array(3) {
          ["group_id"]=&gt;
          int(2)
          ["name"]=&gt;
          string(13) "SecondLevel"
          ["parents"]=&gt;
          array(1) {
            [0]=&gt;
            array(3) {
              ["group_id"]=&gt;
              int(1)
              ["name"]=&gt;
              string(11) "TopLevel"
              ["parents"]=&gt;
              NULL
            }
          }
        }
      }
    }
  </code></pre>
  
  <p>As you can see, the tree structure is in reversed order. I want to rebuild the tree in the correct order with children as subnodes. It should look like this:</p>
  
  <pre><code>array(5) {
    [0]=&gt;
    array(3) {
      ["group_id"]=&gt;
      int(1)
      ["name"]=&gt;
      string(11) "Toplevel"
      ["children"]=&gt;
      NULL
    }
    [1]=&gt;
    array(3) {
      ["group_id"]=&gt;
      int(1)
      ["name"]=&gt;
      string(13) "TopLevel"
      ["children"]=&gt;
      array(1) {
        [0]=&gt;
        array(3) {
          ["group_id"]=&gt;
          int(2)
          ["name"]=&gt;
          string(11) "SecondLevel"
          ["children"]=&gt;
          NULL
        }
      }
    }
    [2]=&gt;
    array(3) {
      ["group_id"]=&gt;
      int(1)
      ["name"]=&gt;
      string(12) "TopLevel"
      ["children"]=&gt;
      array(2) {
        [0]=&gt;
        array(3) {
          ["group_id"]=&gt;
          int(2)
          ["name"]=&gt;
          string(13) "SecondLevel"
          ["children"]=&gt;
          array(1) {
            [0]=&gt;
            array(3) {
              ["group_id"]=&gt;
              int(4)
              ["name"]=&gt;
              string(11) "ThirdLevel"
              ["children"]=&gt;
              NULL
            }
          }
        }
      }
    }
  </code></pre>
  
  <p>I just don't know how to do this. Is there any simple function that I can use, such as uasort?</p>
  
  <p><strong>Update to answer questions:</strong></p>
  
  <p>The array is built like this for this reason:</p>
  
  <ul>
  <li>User A can be member of different groups</li>
  <li>Groups can be member of different subgroups</li>
  <li>Subgroups can be member of different subsubgroups (and so on)</li>
  </ul>
  
  <p>Now User A is in group 1 and 6. And 6 is a subgroup of 5, which is a subgroup of 4, which is a subgroup of 3. This means, that User A is also member of the groups 5,4,3. To fetch every group that is associated with User A, I'll look for each group he is direct member of (1 and 6) and lookup all the parents of these groups (results in: 1,3,4,5,6) with a recursive function:</p>
  
  <pre><code>private function getParentsArray($group_id) {
      $ret=null;
      $parents = $this-&gt;getGroupParents($group_id);
  
      if(!is_null($parents[0])) {
          foreach($parents as $parent) {
              $group = $this-&gt;getGroup($parent);
              if(!empty($this-&gt;getGroupParents($parent)) &amp;&amp; $this-&gt;getGroupParents($parent)[0]) {
                  $ret[] = array("group_id" =&gt; $group["group_id"], "name" =&gt; $group["name"], "parents" =&gt; $this-&gt;getParentsArray($group["group_id"]));
              } else {
                  $ret[] = array("group_id" =&gt; $group["group_id"], "name" =&gt; $group["name"], "parents" =&gt; null);
              }
          }
      }
      return $ret;
  }
  </code></pre>
  
  <p><strong>update 2:
  here's var_export of the existing array, as requested:</strong> </p>
  
  <pre><code>array (
    0 =&gt; 
    array (
      'group_id' =&gt; 1,
      'name' =&gt; 'Toplevel',
      'parents' =&gt; NULL,
    ),
    1 =&gt; 
    array (
      'group_id' =&gt; 2,
      'name' =&gt; 'Secondlevel',
      'parents' =&gt; 
      array (
        0 =&gt; 
        array (
          'group_id' =&gt; 1,
          'name' =&gt; 'Toplevel',
          'parents' =&gt; NULL,
        ),
      ),
    ),
    2 =&gt; 
    array (
      'group_id' =&gt; 4,
      'name' =&gt; 'Thirdlevel',
      'parents' =&gt; 
      array (
        0 =&gt; 
        array (
          'group_id' =&gt; 2,
          'name' =&gt; 'Secondlevel',
          'parents' =&gt; 
          array (
            0 =&gt; 
            array (
              'group_id' =&gt; 1,
              'name' =&gt; 'Toplevel',
              'parents' =&gt; NULL,
            ),
          ),
        ),
        1 =&gt; 
        array (
          'group_id' =&gt; 3,
          'name' =&gt; 'Secondlevel2',
          'parents' =&gt; 
          array (
            0 =&gt; 
            array (
              'group_id' =&gt; 1,
              'name' =&gt; 'Toplevel',
              'parents' =&gt; NULL,
            ),
          ),
        ),
      ),
    ),
    3 =&gt; 
    array (
      'group_id' =&gt; 6,
      'name' =&gt; 'Firstlevel',
      'parents' =&gt; NULL,
    ),
    4 =&gt; 
    array (
      'group_id' =&gt; 10,
      'name' =&gt; 'Forthlevel',
      'parents' =&gt; 
      array (
        0 =&gt; 
        array (
          'group_id' =&gt; 9,
          'name' =&gt; 'Thirdlevel2',
          'parents' =&gt; 
          array (
            0 =&gt; 
            array (
              'group_id' =&gt; 7,
              'name' =&gt; 'Secondlevel3',
              'parents' =&gt; 
              array (
                0 =&gt; 
                array (
                  'group_id' =&gt; 6,
                  'name' =&gt; 'Firstlevel',
                  'parents' =&gt; NULL,
                ),
              ),
            ),
          ),
        ),
      ),
    ),
  )
  </code></pre>