PHP目录排序按内容 [英] PHP Sort directories by contents
本文介绍了PHP目录排序按内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图根据各目录中的文本文件的内容进行排序的目录列表。
到目前为止,我展示的目录列表:
$ USER = $ _GET ['用户'];
$ task_list = $ _GET ['名单'];如果($处理=执行opendir(../用户/ $ USER /任务/ $ task_list /))
{
$文件=阵列();
而(假的!==($文件= READDIR($处理)))
{
如果($文件=&放!&放大器; $文件=...&放大器;!&安培;!$文件=的.htaccess)
{
array_push($文件,$文件);
}
}
closedir($处理);
}//显示任务
排序($文件);
的foreach($文件作为$文件)
{
回声$文件;
}
每个目录下有一个文本文件中叫due.txt,我想按照这个文件due.txt的内容进行排序的目录列表。
到目前为止,我曾尝试:
$ USER = $ _GET ['用户'];
$ task_list = $ _GET ['名单'];如果($处理=执行opendir(../用户/ $ USER /任务/ $ task_list /))
{
$文件=阵列();
$ tasksSort =阵列();
而(假的!==($文件= READDIR($处理)))
{
如果($文件=&放!&放大器; $文件=...&放大器;!&安培;!$文件=的.htaccess)
{
$ taskSort =的file_get_contents(../用户/ $ USER /任务/ $ task_list / $文件/ due.txt);
array_push($文件,$文件);
array_push($ tasksSort,$ taskSort);
}
closedir($处理);
} //排序任务和显示器
排序($ tasksSort);
的foreach($文件作为$文件)
{
回声$文件;
}
}
但 $ tasksSort
阵列似乎没有任何内容进行排序...?
解决方案
排序($ tasksSort);
的foreach($ tasksSort为$文件)
{
回声$文件;
}
I am trying to sort a list of directories according to the contents of a text file within each directory.
So far, I am displaying the list of directories:
$user = $_GET['user'];
$task_list = $_GET['list'];
if ($handle = opendir("../users/$user/tasks/$task_list/"))
{
$files = array();
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && $file != ".htaccess")
{
array_push($files, $file);
}
}
closedir($handle);
}
//Display tasks
sort($files);
foreach ($files as $file)
{
echo "$file";
}
Each directory has a text file within it called due.txt, I would like to sort the list of directories according to the contents of this file due.txt.
So far, I have tried:
$user = $_GET['user'];
$task_list = $_GET['list'];
if ($handle = opendir("../users/$user/tasks/$task_list/"))
{
$files = array();
$tasksSort = array();
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && $file != ".htaccess")
{
$taskSort = file_get_contents("../users/$user/tasks/$task_list/$file/due.txt");
array_push($files, $file);
array_push($tasksSort, $taskSort);
}
closedir($handle);
}
//Sort tasks and display
sort($tasksSort);
foreach ($files as $file)
{
echo "$file";
}
}
But the $tasksSort
array doesn't seem to have any content to sort...?
解决方案
sort($tasksSort);
foreach ($tasksSort as $file)
{
echo "$file";
}
这篇关于PHP目录排序按内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文