Variable containing string path for multidimensional array?

I want to take a string, for example

"/test/uri/to/heaven"

and turn it into a multidimensional, nested array, such as:

array(
    'var' => array(
        'www' => array(
            'vhosts' => array()            
        ),
    ),
);

Does anyone have pointers? I looked at Google and search here, but I did not see anything.

+1
source share
1 answer

Here is a quick non-recursive hack:

$url   = "/test/uri/to/heaven";
$parts = explode('/',$url);

$arr = array();
while ($bottom = array_pop($parts)) {        
    $arr = array($bottom => $arr);
}

var_dump($arr);
+3
source

All Articles