PHP Inverting content adding (sorting)
I have this code which will include "template.php" file from开发者_开发知识库 inside each of these folders: "content/templates/id1", "content/templates/id2", "content/templates/id3" etc. etc.
$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');
}
}
This code works pretty well, the problem is I want to inverse the content adding, meaning that I want first "content/templates/id9/template.php" included before "id8/template.php" and so on till the first..
How can I do this by modifying the code above?
Instead of including the files immediately, add them to an array and call 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;
}
精彩评论