简体   繁体   English

使用scandir定制目录列表顺序的最佳方法是什么

[英]what is the best way to customize the order of directory list using scandir

$dir    = $_SERVER['DOCUMENT_ROOT'] . '/images/myfav/';
$dirFiles = scandir($dir);
foreach ( $dirFiles as $k => $v)
{   
    if (is_dir($dir . "/" . $v) && $v != '.' && $v != '..' )
    {
        if ( ($files = @scandir($dir . "/" . $v)) && (count($files) > 2) )  
        {  
            $link = strtolower(str_replace(' ','-',$v));
            echo '<a href="/myfav/' . $link . '" title="' . $v . '"';
            if ($_GET['subpage'] == $link) echo ' class="selected"';
            echo '><span>' . $v . '</span></a>';    
        }  
    }
}

I am listing the directories by using above script, now there is one requirement came up that I need to dislay these directories in customized order. 我使用上面的脚本列出目录,现在有一个要求我需要按自定义顺序放置这些目录。 Some have the priority and should display before others. 有些具有优先权,应该优先显示。

What is the best way to impliment this. 暗示这一点的最佳方法是什么。

One way which I think could be adding digit in front of each directory according to the order and using script split the digit and perform some sort of order. 我认为一种方法可能是根据顺序在每个目录的前面添加数字,并使用脚本将数字分割并执行某种顺序。

However othe way could be define the array in the script with the exact directory names and order digit, and based upone this we can scan the director and somehow mactch the order of in the existing array in script? 但是,可以用确切的目录名称和顺序数字定义脚本中的数组,并基于此,我们可以扫描导演,并以某种方式确定脚本中现有数组的顺序吗?

Instead of echoing them, write the code to an array, with the value to sort by as the array key. 而不是回显它们,而是将代码写入数组,并将要排序的值作为数组键。 Then use ksort() or one of the other PHP sorting functions to sort the array, and then print the array in the newly sorted order. 然后,使用ksort()或其他PHP排序函数之一对该数组进行排序,然后以新排序的顺序打印该数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM