简体   繁体   English

按日期排序多维数组

[英]sort multidimensional array by date

I have this array :- 我有这个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)

I want to sort it by 'date' 我想按“日期”排序

I tried both of the following methods, but the result ( below )is not sorted correctly. 我尝试了以下两种方法,但是结果(以下)未正确排序。

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');

This is the "sorted" array 这是“排序”数组

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)

You need to modify the datetime strings in your $course array in order to make them comparable in the manner that you want. 您需要修改$course数组中的datetime字符串,以使其按照您希望的方式具有可比性。

One (flexible) way to do this is to create DateTime() objects from your datetime strings and compare those. 一种(灵活的)方法是从日期时间字符串创建DateTime()对象并进行比较。

A quick note about datetimes: standard US format m/d/Y uses forward slashes, and standard European format dmY uses hyphens. 关于日期时间的简要说明:美国标准格式m/d/Y使用正斜杠,而欧洲标准格式dmY使用连字符。 Your datetime strings are a mixture of both, using US-style forward slashes with European day/month/year ordering. 您的日期时间字符串是两者的混合,使用美式正斜杠和欧洲日/月/年顺序。

Therefore you'll have to take an additional step to parse each datetime string into a valid DateTime() object before comparing. 因此,在比较之前,您必须采取额外的步骤将每个datetime字符串解析为有效的DateTime()对象。

Static method DateTime::createFromFormat() can help in this regard. 静态方法DateTime::createFromFormat()在这方面可以提供帮助。 For example, given an array called $course : 例如,给定一个名为$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',
    ],
];

You can then apply a callback with usort() which converts the date value of each comparison object into a valid DateTime() objects before comparing them: 然后,您可以使用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);

This yields: 这样产生:

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
        )

)

If your course array is very large, then there may be some overhead in creating DateTime objects on the fly like the example above. 如果您的课程数组很大,那么像上面的示例一样,动态创建DateTime对象可能会有一些开销。 YMMV. 因人而异。 In this case, I'd consider mapping over the array first and create DateTime objects for each entry, and then apply usort() . 在这种情况下,我将考虑首先在数组上进行映射,并为每个条目创建DateTime对象,然后再应用usort()

Note that, with a standard format datetime string, such as European dm/YH:i:s , US m/d/YH:i:s or IS08601 Ymd H:i:s , you can simply pass the datetime string as the first value into the DateTime constructor instead; 请注意,使用标准格式的日期时间字符串,例如European dm/YH:i:s ,US m/d/YH:i:s或IS08601 Ymd H:i:s ,您可以简单地将datetime字符串作为第一个传递值改为DateTime构造函数; eg: 例如:

$dt = new DateTime($someStandardFormatDateTimeString);

Apologies about the errant close vote earlier. 较早前对错误的结束投票表示歉意。 I've removed that now. 我现在已经删除了。

Hope this helps :) 希望这可以帮助 :)

Create a new array and use the index and unix timestamp then it should be a simple asort 创建一个新的数组,并使用该索引和unix timestamp ,那么它应该是一个简单的asort

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

PHP asort PHP分类

How do I get a unix timestamp from PHP date time? 如何从PHP日期时间获取Unix时间戳?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM