0

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 Answers 2

1

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');
Sign up to request clarification or add additional context in comments.

7 Comments

Ah, didn't even think to debug that. That "works" so far, but it appears to have stripped my key names from the outer array, so I'm left with: [0] => 14 [1] => 14 // Used to say [NewEvents] => 14 [16] => Success //Used to say [Result] => Success
Try uasort() @SourceMatters
That did the trick. I'm still getting weird results though. I've tried both return $t1 < $t2; and return $t1 > $t2;. When I use return $t1 > $t2;, and do a print_r, it is printing the highest indexes first. The times are sorted, but the "oldest" datetime is index 13 (but it's printing first). This is causing issues when I go to loop through them using a for loop later. This is what I'm getting: codepad.org/EFwjF3n9
Tried that, but similar results: codepad.org/fSxGQYiR "Newest" datetime is in index "0", but it should be in index "13". If I reverse the greater than sign, it seems to just reverse the print order of the array (same indexes).
Well, don't mix your "objects" arrays with your "stats" (count, etc). The sorting function treats them all the same. And I guess you might need to use usort() after all if you want the keys to be reindexed. It's bed time for me now, so good luck until much later.
|
0

You could also use array_multisort with a "NATURAL" flag.

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

 array_multisort($dateTime, SORT_NATURAL, $array);

Got some help from http://shiflett.org/blog/2011/jun/sorting-multi-dimensional-arrays-in-php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.