dongpingwu8378 2012-04-23 01:48
浏览 10

排序Glob导致逆序并限制为2

I'm trying to make a list of files inside a folder but in reverse order and limiting to 2 (the last 2), there are a lot of good examples so I liked this one... but I'm trying to limit the amount of results, I almost have it but it gives me an error... can you guys help me?

<?php
foreach (glob("../../imagenes/medianas/*") as $files) {
$images[filectime($files)] = $files;
}
krsort($images);
foreach($images as $timestamp => $files) if ($limite++ < 2){

print "<img src='../../imagenes/medianas/". basename($files) ."' title=''>"."<br>";
}
?>

Thanks' a bunch for your help.


Sorry all, OK... I Fixed it, this is how it works... Thanks Jon, nice hint. Here is the code for anyone who has the same issue

<?php
$limite = 0;
foreach (glob("../../imagenes/medianas/*") as $files) {
    $images[filectime($files)] = $files;
}
krsort($images);
foreach($images as $timestamp => $files) if ($limite++ < 2){

   print "<img src='../../imagenes/medianas/". basename($files) ."' title=''>"."<br>";
}
?>
  • 写回答

1条回答 默认 最新

  • du0204 2012-04-23 01:57
    关注

    When you iterate over the glob() array, just add a $count and check it first:

    $limite = 2;
    $count = 0;
    foreach(krsort(glob('...')) as $file)
      if($count++ <= $limite)
        // do what you do with $file
    
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?