English 中文(简体)
保存一系列关于破坏自然秩序的名录
原标题:Sorting an array of directory filenames in descending natural order

我有一部内容目录,将按自然顺序归还。

使用<代码>scandir()和natsort(),但添加<代码>array_reverse(>,没有结果。

我一直在研究如何利用<条码>开启dir()和<条码>readdir(<>/代码>)的组合,以及影响这一结果的任何其他内容。

The items to be sorted are numbered image files. They are to be returned as: 10 9 8 7 and so on, but like from like 1000 999 998 997 ... until 0

Here s my current code:

$dir =  dead_dir/dead_content/ ;
$launcher = scandir($dir);
natsort($launcher);
array_reverse($launcher, false);
foreach ($launcher as $value) {
    if (in_array(pathinfo($value, PATHINFO_EXTENSION), array( png ))) {
        echo  <img src="dead_dir/dead_content/ .$value. " /> 
    }
}
最佳回答
 $dir= dead_dir/dead_content/ ;
 $launcher= scandir($dir);
 natsort($launcher);
 $r_launcher = array_reverse($launcher,true);

 foreach($r_launcher as $value ){
   if(in_array(pathinfo($value, PATHINFO_EXTENSION),array( png ))){
       echo  <img src="dead_dir/dead_content/ .$value. " /> }}
问题回答

问题很简单:array_reverse(<>/code>>未参照修改。 你混淆了<代码>sort(>的功能。 相反,你只需要使用它产生的回报价值。 尽管如此,有更好的办法。 阅读......

由于PHP5.4,rsort ($array, SORT_NATURAL),你在DESC秩序中将阵列,并将连续编号作为数字而不是扼制。 这是一种比较直接和简明的方法,与natsort(> 然后array_reverse()

(Demo(Demo with jpg>>
(
号前用静态扼杀。

我建议使用<代码>glob()扫描你的名录。 这样,你就可以在同一电话中添加<代码>.png过滤器。

$dir =  dead_dir/dead_content/ ;
$filepaths = glob($dir .  *.png );
rsort($filepaths, SORT_NATURAL);
foreach ($filepaths as $filepath) {
    echo  <img src="  . $filepath .  " /> ;
}

如果你不走这条路,只是归还档案名称,那么你就只是修改了c>urrent w >。

chdir( dead_dir/dead_content );
$filenames = glob( *.png );
rsort($filenames, SORT_NATURAL);
foreach ($filenames as $filename) {
    echo "<div>.png filename => $filename</div>";
}

现在,如果你要求更专业地处理档案名称,那么习惯分类功能会很好地服务于你。

如以下几页所示,航天运营商将自动地将黄麻数字载体作为分类,并得出与以上<代码>代码>解决方案相同的结果。 使用空间运营商仍然比使用natsort(>)更直接。

法典:Demo

$filenames = ["10", "1", "100", "1000", "20", "200", "2"];
usort($filenames, function($a, $b) {
    return $b <=> $a;
});

var_export($filenames);

产出:

array (
  0 =>  1000 ,
  1 =>  200 ,
  2 =>  100 ,
  3 =>  20 ,
  4 =>  10 ,
  5 =>  2 ,
  6 =>  1 ,
)

如果您的档案名称导致或显示非数字特性,则你可以进行必要的操纵,在<条码>(<>tort()内进行比较时,去除不想要的特性。

如果任何人不熟悉定制如何与空间运营商合作......

  • To achieve ASC order, write $a <=> $b.
  • To achieve DESC order, write $b <=> $a.

If your image names will be in the format 123-image_name.jpg, 2323-image_name.jpg, ... this will do:

/**
 * Compares digits in image names in the format "123-image_name.jpg"
 *
 * @param string $img1 First image name
 * @param string $img2 Second image name
 * @return integer -1 If first image name digit is greater than second one.
 * 0 If image name digits are equal.
 * 1 If first image name digit is smaller than second one.
 */
function compareImageNames($img1, $img2){
    $ptr =  /^(d+)-/ ; // pattern

    // let s get the number out of the image names
    if (preg_match($ptr, $img1, $m1) && preg_match($ptr, $img2, $m2)) {
        $first  = (int) $m1[0]; // first match integer
        $second = (int) $m2[0]; // second match integer

        // equal don t change places
        if($first === $second) return 0;

        // if move first down if it is lower than second
        return ($first < $second) ? 1 : -1;
    } else{
        // image names didn t have a digit in them
        // move them to front
        return 1;
    }
}

// sort array
usort($images,  compareImageNames );




相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...

热门标签