0

我有一个数组如下。我只是想按日期订购它的回声,所以我们在顶部有最近的日期。我不确定是否。php 可以对数组进行排序,并且 b. 它是否可以读取其格式的日期。我可以更改捕获 din 的格式,如果这样会更容易?

Array (
    [0] => Array (
    [month_year] => September2012
    [vimeo_link] => http://vimeo.com/472
    )
    [1] => Array (
    [month_year] => July2012
    [vimeo_link] => https://vimeo.com/460
    )
    [2] => Array (
    [month_year] => August2012
    [vimeo_link] => https://vimeo.com/490
    )

)
foreach($rows as $row){?>

    <li><a target="_blank" href="<?php echo $row['vimeo_link'];?>"><?php echo $row['month_year'];?></a></li>            
<?php
    }?>
4

2 回答 2

1

你可以试试

$rows = Array(
        "0" => Array("month_year" => "September2012","vimeo_link" => "http://vimeo.com/472"),
        "1" => Array("month_year" => "July2012","vimeo_link" => "https://vimeo.com/460"),
        "2" => Array("month_year" => "August2012","vimeo_link" => "https://vimeo.com/490"));

usort($rows, function ($a, $b) {
    $a = DateTime::createFromFormat("FY", $a['month_year']);
    $b = DateTime::createFromFormat("FY", $b['month_year']);
    return ($a == $b) ? 0 : (($a < $b) ? - 1 : 1);
});


foreach ( $rows as $row ) {
    printf("<li><a target=\"_blank\" href=\"%s\">%s</a></li>", $row['vimeo_link'], $row['month_year']);
}

输出

  • 2012 年 7 月
  • 2012 年 8 月
  • 2012 年 9 月
  • 于 2012-10-09T09:02:56.660 回答
    0

    您需要将这些月份/日期字符串转换为有效的日期或时间戳,然后您需要查看usort()

    http://php.net/manual/en/function.usort.php

    于 2012-10-09T09:01:15.853 回答