我有这个代码,其中包含每个文件夹中的“template.php”文件:“content / templates / id1”,“content / templates / id2”,“content / templates / id3”等。
$page_file = basename(__FILE__, ".php");
require("content/" . $page_file . "/content.php");
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($page_path),
RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $file) {
if($file->isDir()) {
include strtoupper($file . '/template.php');
}
}
这段代码工作得很好,问题是我想反向添加内容,这意味着我首先要在“id8 / template.php”之前包含“content / templates / id9 / template.php”等等,直到第一..
如何修改上面的代码呢?
答案 0 :(得分:2)
不要立即包含文件,而是将它们添加到数组中并调用array_reverse
$page_file = basename(__FILE__, ".php");
require("content/" . $page_file . "/content.php");
$includes = array();
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($page_path),
RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $file) {
if($file->isDir()) {
$includes[] = strtoupper($file . '/template.php');
}
}
$includes = array_reverse($includes);
foreach($includes as $file){
include $file;
}