duandi8838 2013-02-07 23:44
浏览 26
已采纳

php include_once反向alphabaticly?

I'm currently using this piece of code to load news feeds to my webpage by adding a new html file for every article how can i include files in reverse alphabetical order, So that 2013-02-07 goes before 2013-02-08?

<?php 
   foreach (glob("news/*.html") as $filename)
{
    include_once $filename;
}  ?>  
  • 写回答

1条回答 默认 最新

  • douyiavxxh02727 2013-02-07 23:46
    关注
    $files = glob('news/*.html');
    rsort($files);
    foreach ($files as $f) {
        include $f;
    }
    

    This actually does a reverse lexicographical sort (since the file names are strings), but that luckily works in this case since the dates are in highest to lowest magnitude order. More information: rsort.

    Note: I'm assuming your dates are formatted like YYYY-MM-DD. If they're formatted without leading zeroes, this solution will not work. You will have to extract the date from each file name, change it to some kind of intermediary type (unix timestamp, or a lexicographically sortable string) and then sort on that. For example '2013-3-1' > '2013-12-1' is considered true, but '2013-03-01' > '2013-12-01' is false.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?