按日期顺序PHP数组? [英] PHP order array by date?

查看:400
本文介绍了按日期顺序PHP数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date\">PHP排序方式包含元素日期多维数组

我从XML或JSON的一些数据在PHP数组,看起来像这样:

I have some data from XML or JSON in a PHP array that looks like this:

[0]= array(2) {
    ["title"]= string(38) "Another title"
    ["date"]= string(31) "Fri, 17 Jun 2011 08:55:57 +0200"
}
[1]= array(2) {
    ["title"]= string(38) "My title"
    ["date"]= string(31) "Mon, 16 Jun 2010 06:55:57 +0200"
}

我想要做的就是为了这两个项目按日期。


  1. 是否有可能按日期排序,当排序值是每一个项目?
  2. 里面
  3. 请我需要转换的日期格式为timestamp?

我不想做

我可以使用日期,并将其设置为ID,但不觉得不对劲,因为两个项目可以有相同的日期,然后它不会是唯一的。

I could use date and set it as the ID but that don't feel right, because two items can have the same date and then it would not be unique.

推荐答案

您不需要您的日期排序前时间戳进行转换,但它是一个不错的主意,但因为这将需要更多的时间来排序离不开它。

You don't need to convert your dates to timestamp before the sorting, but it's a good idea though because it will take more time to sort without it.

$data = array(
    array(
        "title" => "Another title",
        "date"  => "Fri, 17 Jun 2011 08:55:57 +0200"
    ),
    array(
        "title" => "My title",
        "date"  => "Mon, 16 Jun 2010 06:55:57 +0200"
    )
);

function sortFunction( $a, $b ) {
    return strtotime($a["date"]) - strtotime($b["date"]);
}
usort($data, "sortFunction");
var_dump($data);

这篇关于按日期顺序PHP数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆