本文介紹了包含路徑作為多維數(shù)組的字符串的變量?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在尋找一個字符串,例如
I'm looking to take a string such as
"/test/uri/to/heaven"
并將其轉(zhuǎn)換為多維嵌套數(shù)組,例如:
and turn it into a multi-dimensional, nested array such as:
array(
'var' => array(
'www' => array(
'vhosts' => array()
),
),
);
有大佬指點一下嗎?我已經(jīng)瀏覽過谷歌和這里的搜索,但我什么也沒看到.
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) {
}
}
}
}
}
這篇關(guān)于包含路徑作為多維數(shù)組的字符串的變量?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!