我对编程很陌生,我一直在尝试做一些事情,这似乎很简单,完成了,但这花了我太长时间,而且我不觉得我已经接近了......
我正在尝试格式化一个如下所示的数组:
Array (
[1] = "index.php"
[2] = "page.php"
[3] = "sub/subpage.php"
[4] = "sub/subpage2.php"
[5] = "sub/subsub/subsubpage.php"
[6] = "sub/subsub/subsubpage2.php"
[7] = "sub2/sub2page.php"
)
进入一个看起来像这样的数组:
Array (
[/] => Array (
[0] => "index.php"
[1] => "page.php"
)
[/sub] => Array (
[0] => "subpage.php"
[1] => "subpage2.php"
[/subsub] => Array (
[0] => "subsubpage.php"
[1] => "subsubpage2.php"
)
)
[/sub2] => Array (
[0] => "sub2page.php"
)
)
我希望这个例子展示了我正在尝试做的事情......这基本上是将我的原始(简单)数组重新格式化为一个数组,我可以使用它在 HTML 中创建某种导航(使用嵌套的 ul)
提前致谢!
编辑:
我试过这个来创建多维数组......
$parts = explode('/', trim($page["parent"], "/"));
while ( !empty($parts) ) {
$pageList[array_pop($parts)] = $page["filename"];
}
// $page = array("filename" => "example.php", "parent" => "sub/sub/")