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 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大