PHP Array Add Regardless Of Children?
<p>I have an array that I populate with parents, children, children of children, children of children of children and so on and so forth. However I can't seem to figure out how to do that for as many levels as their are without having to write each level out.</p>

<p>First now I have</p>

<pre><code>foreach($this-&gt;tree as $k=&gt;$v) {
    if($v['id'] == $i['pid']) {
      // Add children
      $this-&gt;tree[$k]['children'][] = array('name'=&gt;$i['name'],'id'=&gt;$i['id']);
      break;
    }else{
      foreach($v['children'] as $kc=&gt;$vc) {
        $this-&gt;tree[$k]['children'][$kc]['children'][] = array('name'=&gt;$i['name'],'id'=&gt;$i['id']);
      }
    }
}
</code></pre>

<p>Which generates something that looks like</p>

<pre><code>Array
(
    [0] =&gt; Array
        (
            [name] =&gt; Test
            [id] =&gt; 1
            [children] =&gt; Array
                (
                    [0] =&gt; Array
                        (
                            [name] =&gt; Test2
                            [id] =&gt; 2
                            [children] =&gt; Array
                                (
                                    [0] =&gt; Array
                                        (
                                            [name] =&gt; Test 3
                                            [id] =&gt; 3
                                        )

                                )

                        )

                )

        )

);
</code></pre>

<p>But I can't seem to figure out how to do is without writing out a billion foreach loops.</p>

<p>$i is just four rows, id, pid (parent id), name, and weight.</p>
