Php 按自然顺序对图像排序?

Php 按自然顺序对图像排序?,php,Php,这是我的PHP代码块 <?php 我假设我应该在代码继续执行到这一点之前使用natsort() echo "<b><font size='$font_size'>".$gallery_name."</font></b><br>"; $loop = "0"; while (sizeof($string) != 0){ $img = array_pop($string); echo.“$gallery\u name.”;

这是我的PHP代码块

<?php
我假设我应该在代码继续执行到这一点之前使用natsort()

echo "<b><font size='$font_size'>".$gallery_name."</font></b><br>";
$loop = "0";
while (sizeof($string) != 0){
    $img = array_pop($string);
echo.“$gallery\u name.”
; $loop=“0”; while(sizeof($string)!=0){ $img=array\u pop($string);
我可以在这里使用natsort()吗

echo“



”; $loop=$loop+1; 如果($loop==$loop\u end){ 回声“
”; $loop=“0”; } } ?>

如何按自然顺序对图像进行排序?

创建$string[]数组后,现在可以对其进行排序

它将在适当的位置进行排序,即不必将结果分配给另一个变量,函数将返回true或false(失败时)

while($file=readdir($dir)){
if(eregi(\.png,$file)| eregi(\.jpg,$file)| eregi(\.gif,$file)){
$string[]=$file;
}
}
//打印(字符串);
natsort($string);
//打印(字符串);
//然后依次显示;
foreach($img形式的字符串){

echo“谢谢大家的意见!你的建议对我帮助很大

<?php

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

再次感谢大家!

换句话说:您想从一个文件夹中抓取具有以下扩展名之一的所有图像:
.jpg、.png或.gif
,对它们进行自然排序,然后显示它们?这段代码对您有用吗:(只需为您调整
glob()
中的路径,例如
your/path/*.
)?嗨,我已经通过添加natsort($string)解决了对代码的需求;然后使用array_reverse($string)另外反转顺序;非常感谢您的时间和考虑!但是您的代码已被弃用。强烈建议不要使用这些函数!因此,也许您想看看我的代码是否有效谢谢您提供的信息。我将尝试一下您的代码片段。我非常感谢您的宝贵贡献!这就是我所需要的,在您的帮助下进行连接natsort();到natsort($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";
    }
}

?>
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 ...";
    ...
}
<?php

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

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";
    }
}

?>