我有一个像这样的字符串:-
dev/clients/518/aaa/1915/bbb/1/file.pdf
我的目标是能够通过多级对象引用file.pdf
dev->clients->518->aaa->1915->bbb->1
或数组
[dev][clients][518][aaa][1915][bbb][1]
这是AWS S3的密钥,我正在尝试从中创建可导航的结构。我可以将字符串爆炸
Array
(
[0] => dev
[1] => clients
[2] => 518
[3] => aaa
[4] => 1915
[5] => bbb
[6] => 1
[7] =>file.pdf
)
不确定如何从此处继续前进。这可能显示出我的追求会更好:-
Array (
[dev] => Array (
[clients] => Array (
[518] => Array (
[aaa] => Array (
[1915] => Array (
[bbb] => Array (
[1] => file.pdf
)
)
)
)
)
)
)
答案 0 :(得分:0)
您可以使用简单的for-loop
来做到这一点:
$arr = explode("/", "dev/clients/518/aaa/1915/bbb/1/file.pdf");
$arr = array_reverse($arr); //reverse the order to start from the lowest level
$res = array_shift($arr); //init the result as the last element ("file")
foreach($arr as $e) {
$tmp[$e] = $res;
$res = $tmp;
unset($tmp[$e]); //so $tmp will not contain dragging element
}
您希望输出在$res