Recursive array structured
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/8537148/string-with-array-structure-to-array">String with array structure to Array</a>  </p>
</blockquote>



<p>I have an array as such</p>

<pre><code>$foo = array();
$foo['/'] = 'value';
$foo['/foo'] = 'value';
$foo['/foo/bar'] = 'value';
$foo['/test'] = 'value';
$foo['/test/tester'] = 'value';
$foo['/hello'] = 'value';
$foo['/hello/world'] = 'value';
$foo['/hello/world/blah'] = 'value';
</code></pre>

<p>What I need to do is store these sub-pages in a tree-like structure, so instead it needs to be automatically converted to:</p>

<pre><code>   $foo = array(
    '/' =&gt; array(
        'value' =&gt; 'value',
        'children' =&gt; array(
            '/foo' =&gt; array(
                'value' =&gt; 'value',
                'children' =&gt; array(
                    '/foo/bar' =&gt; array(
                        'value' =&gt; 'value',
                        'children' =&gt; array()
    );
</code></pre>

<p>What I figured I would be doing, is something like:</p>

<pre><code>$newArray = array();
foreach( $foo as $key =&gt; $val )
{
    $bits = explode('/', $key);

    foreach( $bits as $bit )
    {
        $newArray[$bit] = array('val' =&gt; $val);
    }
}

print_r($newArray);
</code></pre>

<p>Except I somehow need to go into the newArray, and keep track of how deep I am into the array.  Does anyone have an example script of how they have done so, or have any funky array walk tips to do so?</p>
