【问题标题】:Sort directory list排序目录列表
【发布时间】:2013-09-25 21:59:41
【问题描述】:

我想对此脚本的输出进行排序。

它显示了一个包含一些排除项的完整目录的输出。

<?php
if ($handle = opendir('.')) {
    $blacklist = array('.', '..', '.svn', 'pagina.php','index.php','index.html');

echo "<h1>Statistics for:</h1>";
echo "<TABLE align=left border=1 cellpadding=5 cellspacing=0 class=whitelinks>";

 while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {

                echo "<TR><TD><a href='./$file'>$file\n</a></TD></TR>";
        }
    }
    closedir($handle);

echo"</TABLE>";
}
?>

我想做的是对字母表目录中的输出进行排序。 A --> B --> C ...

我该如何解决这个问题。我已经尝试过排序。但我可以让它工作

【问题讨论】:

    标签: php sorting while-loop directory output


    【解决方案1】:

    先将$file放入数组中,在数组上使用sort,然后打印出html

    $files = array();
    while (false !== ($file = readdir($handle))) {
       if (!in_array($file, $blacklist)) {
          $files[] = $file;
       }
    }
    
    sort($files,SORT_NATURAL); //use natsort if php is below 5.4
    
    foreach($files as $file) {
       echo "<TR><TD><a href='./$file'>$file\n</a></TD></TR>";
    }
    

    【讨论】:

    • @MitchelPersoon "won't work" 是什么意思,你得到一个错误,排序没有发生吗?
    猜你喜欢
    • 2019-03-11
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 2011-02-25
    • 2022-11-12
    相关资源
    最近更新 更多