按日期排序多维数组

时间:2016-06-29 08:00:22

标签: php

我有array: -

array (size=8)
  0 => 
    array (size=2)
      'date' => string '17/05/2016 00:00:00' (length=19)
      'reason' => string 'DNA' (length=3)
  1 => 
    array (size=2)
      'date' => string '10/05/2016 00:00:00' (length=19)
      'reason' => string 'UTA' (length=3)
  2 => 
    array (size=2)
      'date' => string '03/05/2016 00:00:00' (length=19)
      'reason' => string 'DNA' (length=3)
  3 => 
    array (size=2)
      'date' => string '26/04/2016 00:00:00' (length=19)
      'reason' => string 'true' (length=4)
  4 => 
    array (size=2)
      'date' => string '31/05/2016 00:00:00' (length=19)
      'reason' => string 'true' (length=4)
  5 => 
    array (size=2)
      'date' => string '24/05/2016 00:00:00' (length=19)
      'reason' => string 'true' (length=4)
  6 => 
    array (size=2)
      'date' => string '07/06/2016 00:00:00' (length=19)
      'reason' => string 'true' (length=4)
  7 => 
    array (size=2)
      'date' => string '14/06/2016 00:00:00' (length=19)
      'reason' => string 'true' (length=4)

我想按照' date'

对其进行排序

我尝试了以下两种方法,但结果(如下)未正确排序。

usort($course, function($a, $b) {
    return $a['date'] - $b['date'];
});

_______________________________
function date_compare($a, $b)
{
    $t1 = strtotime($a['date']);
    $t2 = strtotime($b['date']);
    return $t1 - $t2;
}
usort($course, 'date_compare');

这是"排序"阵列

array (size=8)
  0 => 
    array (size=2)
      'date' => string '24/05/2016 00:00:00' (length=19)
      'reason' => string 'true' (length=4)
  1 => 
    array (size=2)
      'date' => string '14/06/2016 00:00:00' (length=19)
      'reason' => string 'true' (length=4)
  2 => 
    array (size=2)
      'date' => string '31/05/2016 00:00:00' (length=19)
      'reason' => string 'true' (length=4)
  3 => 
    array (size=2)
      'date' => string '26/04/2016 00:00:00' (length=19)
      'reason' => string 'true' (length=4)
  4 => 
    array (size=2)
      'date' => string '17/05/2016 00:00:00' (length=19)
      'reason' => string 'DNA' (length=3)
  5 => 
    array (size=2)
      'date' => string '03/05/2016 00:00:00' (length=19)
      'reason' => string 'DNA' (length=3)
  6 => 
    array (size=2)
      'date' => string '07/06/2016 00:00:00' (length=19)
      'reason' => string 'true' (length=4)
  7 => 
    array (size=2)
      'date' => string '10/05/2016 00:00:00' (length=19)
      'reason' => string 'UTA' (length=3)

2 个答案:

答案 0 :(得分:2)

您需要修改$course数组中的日期时间字符串,以便以您希望的方式对其进行比较。

一种(灵活的)方法是从日期时间字符串创建DateTime()个对象并进行比较。

关于日期时间的快速说明:标准美国格式m/d/Y使用正斜杠,标准欧洲格式d-m-Y使用连字符。您的日期时间字符串是两者的混合,使用美式正斜杠和欧洲日/月/年订购。

因此,在进行比较之前,您必须采取额外步骤将每个日期时间字符串解析为有效的DateTime()对象。

静态方法DateTime::createFromFormat()可以在这方面提供帮助。例如,给定一个名为$course的数组:

$course = [
    [
        'date' => '17/05/2016 00:00:00',
        'reason' => 'DNA',
    ],
    [
        'date'   => '10/05/2016 00:00:00',
        'reason' => 'UTA',
    ],
    [
        'date'   => '03/05/2016 00:00:00',
        'reason' => 'DNA',
    ],
    [
        'date'   => '26/04/2016 00:00:00',
        'reason' => 'true',
    ],
    [
        'date'   => '31/05/2016 00:00:00',
        'reason' => 'true',
    ],
    [
        'date'   => '24/05/2016 00:00:00',
        'reason' => 'true',
    ],
    [
        'date'   => '07/06/2016 00:00:00',
        'reason' => 'true',
    ],
    [
        'date'   => '14/06/2016 00:00:00',
        'reason' => 'true',
    ],
];

然后,您可以使用usort()应用回调,该回调会将每个比较对象的date值转换为有效的DateTime()对象,然后再进行比较:

usort($course, function ($a, $b) {
    $dateA = DateTime::createFromFormat('d/m/Y H:i:s', $a['date']);
    $dateB = DateTime::createFromFormat('d/m/Y H:i:s', $b['date']);
    // ascending ordering, use `<=` for descending
    return $dateA >= $dateB;
});

print_r($course);

这会产生:

Array
(
    [0] => Array
        (
            [date] => 26/04/2016 00:00:00
            [reason] => true
        )

    [1] => Array
        (
            [date] => 03/05/2016 00:00:00
            [reason] => DNA
        )

    [2] => Array
        (
            [date] => 10/05/2016 00:00:00
            [reason] => UTA
        )

    [3] => Array
        (
            [date] => 17/05/2016 00:00:00
            [reason] => DNA
        )

    [4] => Array
        (
            [date] => 24/05/2016 00:00:00
            [reason] => true
        )

    [5] => Array
        (
            [date] => 31/05/2016 00:00:00
            [reason] => true
        )

    [6] => Array
        (
            [date] => 07/06/2016 00:00:00
            [reason] => true
        )

    [7] => Array
        (
            [date] => 14/06/2016 00:00:00
            [reason] => true
        )

)

如果您的课程数组非常大,那么在动态创建DateTime对象时可能会有一些开销,如上例所示。因人而异。在这种情况下,我会考虑先在数组上进行映射,然后为每个条目创建DateTime个对象,然后应用usort()

请注意,使用标准格式的日期时间字符串,例如欧洲d-m/Y H:i:s,美国m/d/Y H:i:s或IS08601 Y-m-d H:i:s,您只需将日期时间字符串作为第一个值传递给{ {1}}构造函数; e.g:

DateTime

对此前错误的近距离投票表示歉意。我现在已经删除了。

希望这会有所帮助:)

答案 1 :(得分:0)

创建一个新数组并使用索引unix timestamp然后它应该是一个简单的asort

$unix_time = date('Ymdhis', strtotime($datetime ));

PHP asort

How do I get a unix timestamp from PHP date time?