PHP自定义排序多维数组的日期时间字段


PHP Custom Sort Multi-Dimensional Array by DateTime Field

我搜索了这个,似乎与此类似的都将是基于海报的原始数组结构的"自定义"。

我正在努力获得下面的数组按每个条目的[DateTime]键排序。我希望它们升序排序(最新日期时间与最高的数组索引),并希望保留数组结构和键,如果可能的话。

Array
(
[0] => Array
    (
        [Source] => SimpleXMLElement Object
            (
                [0] => RTU-12 Merchandise Support, Fan Status Switch
            )
        [EventType] => SimpleXMLElement Object
            (
                [0] => Alarm Recall
            )
        [Description] => SimpleXMLElement Object
            (
                [0] => No Flow
            )
        [DateTime] => 07-25-2015 20:09:47
        [Priority] => SimpleXMLElement Object
            (
                [0] => Medium
            )
        [SubSystemKey] => SimpleXMLElement Object
            (
                [0] => 2
            )
        [ViewKey] => SimpleXMLElement Object
            (
                [0] => 7
            )
    )
[1] => Array
    (
        [Source] => SimpleXMLElement Object
            (
                [0] => RTU-03 Checkout Area, Fan Status Switch
            )
        [EventType] => SimpleXMLElement Object
            (
                [0] => Alarm Recall
            )
        [Description] => SimpleXMLElement Object
            (
                [0] => No Flow
            )
        [DateTime] => 07-25-2015 20:09:44
        [Priority] => SimpleXMLElement Object
            (
                [0] => Medium
            )
        [SubSystemKey] => SimpleXMLElement Object
            (
                [0] => 2
            )
        [ViewKey] => SimpleXMLElement Object
            (
                [0] => 7
            )
    )

[12] => Array
    (
        [Source] => SimpleXMLElement Object
            (
                [0] => ~RackA'SGr2'Cmp4, Proof of Running
            )
        [EventType] => SimpleXMLElement Object
            (
                [0] => Alarm Recall
            )
        [Description] => SimpleXMLElement Object
            (
                [0] => No Proof
            )
        [DateTime] => 07-25-2015 19:39:13
        [Priority] => SimpleXMLElement Object
            (
                [0] => Medium
            )
        [SubSystemKey] => SimpleXMLElement Object
            (
                [0] => 1
            )
        [ViewKey] => SimpleXMLElement Object
            (
                [0] => 2
            )
    )
[13] => Array
    (
        [Source] => SimpleXMLElement Object
            (
                [0] => ~RackC'SGr1, Suction Pressure
            )
        [EventType] => SimpleXMLElement Object
            (
                [0] => Alarm
            )
        [Description] => SimpleXMLElement Object
            (
                [0] => Pressure too high
            )
        [DateTime] => 07-25-2015 19:14:21
        [Priority] => SimpleXMLElement Object
            (
                [0] => Medium
            )
        [SubSystemKey] => SimpleXMLElement Object
            (
                [0] => 1
            )
        [ViewKey] => SimpleXMLElement Object
            (
                [0] => 4
            )
    )
[Count] => 14
[NewEvents] => 14
[Result] => Success
)

以下是我到目前为止所做的尝试:

function date_compare($a, $b)
{
    $t1 = strtotime($a['DateTime']);
    $t2 = strtotime($b['DateTime']);
    return $t1 > $t2;
}    
usort($alarms, 'date_compare');

我的结果只是一个未排序的(似乎是破碎的)数组。我不太擅长使用usort,所以需要一些指导。

谢谢!

似乎strtotime()不解析这个日期格式:07-25-2015 19:39:13,这是通过一些快速实验证实的:

var_dump(strtotime("07-25-2015 19:39:13"));

bool (false)

var_dump(strtotime("07/25/2015 19:39:13"));

int (1437845953)

strtotime()可用日期格式的详细列表在这里:
http://php.net/manual/en/datetime.formats.date.php

解决这个问题的最快方法是将破折号转换成斜杠:

function date_compare($a, $b) {
    $t1 = strtotime(str_replace('-', '/', $a['DateTime']));
    $t2 = strtotime(str_replace('-', '/', $b['DateTime']));
    return $t1 > $t2;
}
usort($alarms, 'date_compare');

您可能希望使用uasort()来保存数组的键。
http://php.net/manual/en/function.uasort.php

如果认为第一个参数分别小于、等于或大于第二个参数,比较函数必须返回小于、等于或大于零的整数。

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

因此:

function date_compare($a, $b) {
    $t1 = strtotime(str_replace('-', '/', $a['DateTime']));
    $t2 = strtotime(str_replace('-', '/', $b['DateTime']));
    return $t1 > $t2 ? -1 : 1;
}
uasort($alarms, 'date_compare');

您也可以使用带有"NATURAL"标志的array_multisort。

$dateTime = array();
foreach ($array as $tempArray) {         
    $dateTime[] = $tempArray["DateTime"];    
 }
 array_multisort($dateTime, SORT_NATURAL, $array);

得到了一些帮助http://shiflett.org/blog/2011/jun/sorting-multi-dimensional-arrays-in-php