按字典顺序排序PHP

时间:2014-01-27 18:28:50

标签: php html

我创建了一个脚本,该脚本正在上传图像,并在目录中有多少图像后命名。就好如果目录中有一个图像,它将被命名为0而14:14就是14.我还创建了一个脚本,它将图像显示在网站上,最新图像位于顶部,最旧图像位于底部,使用" array_reverse()"。

订单最多可处理10张图像(请记住第一张图像为0),但由于按字典顺序排列,第11张图像显示在1和2之间。

如何在不使用数据库的情况下阻止这种情况?

非常适合任何穿着!

修改

我在这里有一个例子:

    <?php
    $title = "Click to see the full size image!";
    //upload from folder
    error_reporting(0);
    $files = glob("images/*.*");
    $files = array_reverse($files);


    for ($i=0; $i<count($files); $i++)
    {
    $image = $files[$i];
    echo '<a href="'.$image.'"><img src="'.$image.'" width="400px" height="300px"   title="'.$title.'"></a>';
    }

&GT;

2 个答案:

答案 0 :(得分:4)

我认为你在谈论&#34;自然排序&#34;或自然分类。这是内置的PHP。看看这个例子(碰巧处理图像名称!)

http://php.net/natsort

<?php
    $title = "Click to see the full size image!";
    //upload from folder
    error_reporting(0);
    $files = glob("images/*.*");
    natsort($files);


    for ($i=0; $i<count($files); $i++)
    {
    $image = $files[$i];
    echo '<a href="'.$image.'"><img src="'.$image.'" width="400px" height="300px"   title="'.$title.'"></a>';
    }
?>

答案 1 :(得分:0)

您可以使用natsort命令将natural ordering应用于列表中的项目。

例如:

<?php
  $array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png");
  natsort($array2);
  print_r($array2);
?>

给出:

Array
(
    [3] => img1.png
    [2] => img2.png
    [1] => img10.png
    [0] => img12.png
)