PHP Reorder array to reflect parent / id hierarchy
<p>How do I rearrange the following array</p>

<pre><code>[0] =&gt; Array
    (
        [id] =&gt; 1
        [parent_id] =&gt; 0
        [name] =&gt; Accueil
    )

[1] =&gt; Array
    (
        [id] =&gt; 2
        [parent_id] =&gt; 0
        [name] =&gt; Exposants
    )

[2] =&gt; Array
    (
        [id] =&gt; 3
        [parent_id] =&gt; 0
        [name] =&gt; Visiteurs
    )

[3] =&gt; Array
    (
        [id] =&gt; 4
        [parent_id] =&gt; 0
        [name] =&gt; Medias
    )

[4] =&gt; Array
    (
        [id] =&gt; 5
        [parent_id] =&gt; 0
        [name] =&gt; Activités
    )

[5] =&gt; Array
    (
        [id] =&gt; 6
        [parent_id] =&gt; 1
        [name] =&gt; Contact
    )

[6] =&gt; Array
    (
        [id] =&gt; 7
        [parent_id] =&gt; 3
        [name] =&gt; Partenaires
    )

[7] =&gt; Array
    (
        [id] =&gt; 8
        [parent_id] =&gt; 2
        [name] =&gt; News
    )
</code></pre>

<p>So I come up with an array that reflects the hierarchy as shown by the id and parent_id fields? The array key is the ID field of array elements are parents. Inside this array is each time a child array that has its ID field as the key. Sample:</p>

<pre><code>[1] =&gt; Array
        (
            [name] =&gt; Accueil
            [children] =&gt; array(
                [0] =&gt; bla,
                [3]     =&gt; bla2
            )
        )

    [2] =&gt; Array
        (
            [name] =&gt; Something
            [children] =&gt; array(
                [4] =&gt; bla3,
            )
        )
</code></pre>
