PHP - 对原始排序后的数据进行排序 [英] PHP - Sort data after original sort
问题描述
显示之前...
  ; - 先按文件名排序
- 然后在这种排序中排序
示例:
a_first.jpg
_first.png
a_first.zip
b_second.doc
b_second.gif <
b_second.jpg
<?php
function getFileExt($文件名){
return substr(strrchr($ filename,'。',1);
}
$ handle = opendir(dirname(__ FILE__)); ($ file = readdir($ handle))!== false){
$ file
$ b $
if(in_array($ file,$ ignore_file_list)){continue; }
if(in_array($ fileExt,$ ignore_ext_list)){continue; }
if(is_dir($ file)){$ fileExt =dir; }
$ b / *
这里:
显示之前...
- 先按文件名排序
- 然后按此扩展sort
示例:
a_first.jpg
a_first.png
a_first.zip
b_second.doc
b_second.gif
b_second.jpg
* /
echo'
< div>< a href ='。$ file。'class ='。$ fileExt。'>& NBSP;< / A>< / DIV>
< div>< a href ='。$ file。'> $ file< / a>< / div>
';
}
closedir($ handle);
?>
我不确定是否需要执行第二种排序(=文件扩展名) WHILE使用FOREACH或在WHILE之外进行FOREACH(将数据放入数组中)或者这是一个多数组排序的情况或... ???
您可以使用glob与natcasesort。
$ files = glob($ directory。/*.*);
//仅用于文件名称
$ files = array_map(basename,$ files);
natcasesort($ files);如果您想将结果限制为具有某些扩展名的项目,则可以将您的glob调用更改为
$ b p>
$ files = glob($ directory。/ *。{jpg,png,zip},GLOB_BRACE);
I am displaying a directory listing. I'm trying to get the files sorted by filename but also be sorted by their extension.
Before display...
- sort by filename first
- then sort by extension within this sort
Example:
a_first.jpg
a_first.png
a_first.zip
b_second.doc
b_second.gif
b_second.jpg
<?php
function getFileExt($filename) {
return substr(strrchr($filename,'.'),1);
}
$handle=opendir(dirname(__FILE__));
while (($file = readdir($handle))!==false) {
$fileExt = strtolower(getFileExt($file));
if(in_array($file, $ignore_file_list)) { continue; }
if(in_array($fileExt, $ignore_ext_list)) { continue; }
if(is_dir($file)) { $fileExt = "dir"; }
/*
HERE:
Before display...
- sort by filename first
- then sort by extension within this sort
Example:
a_first.jpg
a_first.png
a_first.zip
b_second.doc
b_second.gif
b_second.jpg
*/
echo '
<div><a href='.$file.' class='.$fileExt.'> </a></div>
<div><a href='.$file.'>$file</a></div>
';
}
closedir($handle);
?>
I'm not sure if I need to do the second sort (= the files' extensions) within the WHILE using a FOREACH or do the FOREACH outside of the WHILE (to put the data into an array first) or is this a multi-array sorting situation or ... ???
FYI: Using a database for this is not an option.
Any help is appreciated.
解决方案 you can use glob along with natcasesort.
$files = glob($directory."/*.*");
// for just the files names
$files = array_map("basename", $files);
natcasesort($files);
If you wanted to limit the results to items with certain extensions you would change your glob call to
$files = glob($directory."/*.{jpg,png,zip}", GLOB_BRACE);
这篇关于PHP - 对原始排序后的数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!