我有一个类似的目录结构路径 root / path1 / path2 / path3 我希望这是
Array(
[0] => "root"
[1] => Array
(
[0] => "path1"
[1] => Array
(
[0] => "path2"
[1] => Array
(
[0] => "path3"
)
)
)
)
答案 0 :(得分:7)
试试:
$input = 'root/path1/path2/path3';
$output = null;
foreach (array_reverse(explode('/', $input)) as $part) {
$output = $output ? array($part, $output) : array($part);
}
var_dump($output);
输出:
array (size=2)
0 => string 'root' (length=4)
1 =>
array (size=2)
0 => string 'path1' (length=5)
1 =>
array (size=2)
0 => string 'path2' (length=5)
1 =>
array (size=1)
0 => string 'path3' (length=5)