包含路径作为多维数组的字符串的变量? [英] Variable containing a path as a string to multi-dimensional array?

查看:21
本文介绍了包含路径作为多维数组的字符串的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个字符串,例如

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.

推荐答案

这是一个快速的非递归黑客:

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

输出:

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

这篇关于包含路径作为多维数组的字符串的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆