加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – 多维数组路径的字符串

发布时间:2021-07-06 05:23:03 所属栏目:大数据 来源: https://www.jb51.cc
导读:我有一个多维数组,这里有一个小摘录: Array ( [Albums] = Array ( [A Great Big World - Is There Anybody Out There] = Array(...), [ATB - Contact] = Array(...), ) [Pop] = Array (...)) 我有一条动态的道路: /albums/a_great_big_world_-_is_there_an

我有一个多维数组,这里有一个小摘录:

Array (
    [Albums] => Array (
        [A Great Big World - Is There Anybody Out There] => Array(...),
        [ATB - Contact] => Array(...),
    )
    [Pop] => Array (...)
)

我有一条动态的道路:

/albums/a_great_big_world_-_is_there_anybody_out_there

什么是检索(在这个例子中)$arr [“专辑”] [“一个伟大的大世界 – 有没有人在那里”]的价值的最佳方法?

请注意,它应该是动态的,因为嵌套可以比本例中的2级更深.

编辑

这是我用来为URL创建一个简单字符串的函数:

function formatURL($url) {
    return preg_replace('/__+/', '_', preg_replace('/[^a-z0-9_s-]/', "", strtolower(str_replace(" ", "_", $url))));
}

解决方法:

$array = array(...);
$path  = '/albums/a_great_big_world_-_is_there_anybody_out_there';

$value = $array;
foreach (explode('/', trim($path, '/')) as $key) {
    if (isset($value[$key]) && is_array($value[$key])) {
        $value = $value[$key];
    } else {
        throw new Exception("Path $path is invalid");
    }
}

echo $value;

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读