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

PHP Custom Sort Multi-Dimensional Array by DateTime Field

我搜了一下,好像类似的都是"custom"根据楼主原来的数组结构

我正在努力让下面的数组按每个条目的 [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

还要考虑这个:

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

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