Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm looking to take a string such as

"/test/uri/to/heaven"

and turn it into a multi-dimensional, nested array such as:

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

Anyone got any pointers? I've had a look through Google and the search here, but I've not seen anything.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
217 views
Welcome To Ask or Share your Answers For Others

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);

Output:

array(1) {
  ["test"]=>
  array(1) {
    ["uri"]=>
    array(1) {
      ["to"]=>
      array(1) {
        ["heaven"]=>
        array(0) {
        }
      }
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...