duan97689 2015-04-27 12:41
浏览 37
已采纳

按自然顺序排序图像?

This is my block of 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.

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?

    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?

  • 写回答

2条回答 默认 最新

  • duanliang1019 2015-04-27 12:52
    关注

    after you have created your $string[] array you can now sort it.

    It will be sorted in place i.e. you don't have to assign a result to another variable, the natsort() function will return true or false (on failure).

    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 ...";
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么