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!