简体   繁体   English

以自然顺序对图像排序?

[英]Sort images in natural order?

This is my block of PHP. 这是我的PHP块。

<?php

I know that this is where the array is defined. 我知道这是定义数组的地方。

$string =array();
$dir = opendir($filePaththumb);
while ($file = readdir($dir)) {
    if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) {
    $string[] = $file;
    }
}

I assume I should use natsort() before the code continues beyond this point. 我认为我应该在代码继续到此为止之前使用natsort()。

echo "<b><font size='$font_size'>".$gallery_name."</font></b><br>";
$loop = "0";
while (sizeof($string) != 0){
    $img = array_pop($string);

Can I use natsort() here? 我可以在这里使用natsort()吗?

    echo "<center><a href='$filePath$img' download='$filePath$img' target='$target_thumb'><img src='$filePaththumb$img' border='0' width='100%'/><BR><IMG src='img/download.png'></a><BR><BR><BR><BR></center>";
    $loop = $loop + 1;
    if ($loop == $loop_end) {
        echo "<br>";
        $loop = "0";
    }
}

?>

How can I sort images in natural order? 如何对图像进行自然排序?

Thanks everyone for you input! 感谢大家的投入! Your suggestions helped me greatly! 您的建议对我有很大帮助!

<?php

$string =array();
$dir = opendir($filePaththumb);
while ($file = readdir($dir)) {
    if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) {
    $string[] = $file;

After the Array has been filled with .jpg/.gif/.png files, I used 在数组中填充.jpg / .gif / .png文件后,我使用了

natsort($string); to arrange the image files in the $string array in Natural Order 自然顺序将图像文件排列在$ string数组中

I also used array_reverse($string); 我还使用了array_reverse($string); to arrange the order of the image files from filename of greatest Natural Order value to descending Natural Order of value. 从最大自然顺序值的文件名到自然值降序排列图像文件的顺序。

    }
}

echo "<b><font size='$font_size'>".$gallery_name."</font></b><br>";
$loop = "0";
while (sizeof($string) != 0){
    $img = array_pop($string);    
    echo "<center><a href='$filePath$img' download='$filePath$img' target='$target_thumb'><img src='$filePaththumb$img' border='0' width='100%'/><BR><IMG src='img/download.png'></a><BR><BR><BR><BR></center>";
    $loop = $loop + 1;
    if ($loop == $loop_end) {
        echo "<br>";
        $loop = "0";
    }
}

?>

Thanks again everyone! 再次感谢大家!

after you have created your $string[] array you can now sort it. 创建$ string []数组后,现在可以对其进行排序。

It will be sorted in place ie you don't have to assign a result to another variable, the natsort() function will return true or false (on failure). 它将在适当位置进行排序即您不必将结果分配给另一个变量, natsort()函数将返回true或false(失败时)。

while ($file = readdir($dir)) {
    if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) {
        $string[] = $file;
    }
}
//print_r($string);
natsort($string);
//print_r($string);

// then display them in order;
foreach ($string as $img){
    echo "<img ...";
    ...
}

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

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