我搜索了这个,似乎与此相似的那些都将成为" 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不太熟练,所以寻找一些指导。
谢谢!
答案 0 :(得分:1)
似乎strtotime()
无法解析此日期格式:07-25-2015 19:39:13
,这已通过一些快速实验得到证实:
var_dump(strtotime("07-25-2015 19:39:13"));
布尔(假)
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');
答案 1 :(得分:0)
你也可以使用带有“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