dongtingrun4973 2015-07-26 01:57
浏览 42
已采纳

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

I searched for this, and it seems the ones similar to this are all going to be "custom" based on the poster's original array structure.

I'm struggling with getting the array below sorted by the [DateTime] key of each entry. I'd like them sorted Ascending (newest datetime with the highest array index), and would like to retain the array structure and keys if possible.

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
            )

    )

... SOME INDICES REMOVED FOR READABILITY ...

[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
)

Here is what I've tried so far:

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

usort($alarms, 'date_compare');

My results are simply an unsorted (and seemingly broken organization) array. I'm not too skilled with usort's, so looking for some guidance.

Thanks!

  • 写回答

2条回答 默认 最新

  • doujiku1028 2015-07-26 02:23
    关注

    It seems strtotime() doesn't parse this date format: 07-25-2015 19:39:13, which is confirmed by some quick experimentation:

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

    bool(false)

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

    int(1437845953)

    A detailed list of date formats usable by strtotime() is available here:
    http://php.net/manual/en/datetime.formats.date.php

    The quickest way to solve this is to convert the dashes into slashes:

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

    You might want to use uasort() to preserve the keys of your array.
    http://php.net/manual/en/function.uasort.php

    Also consider this:

    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

    Therefore:

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?