php 根据php中的日期时间对数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8121241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sort array based on the dateTime in php
提问by Acubi
Array
(
[0] => Array
(
[dateTime] => 2011-10-18 0:0:00
[chanl1] => 20.7
[chanl2] => 45.4
[chanl3] =>
)
[1] => Array
(
[dateTime] => 2011-10-18 0:15:00
[chanl1] => 20.7
[chanl2] => 45.4
[chanl3] =>
)
[2] => Array
(
[dateTime] => 2011-10-18 00:14:00
[chanl1] => 20.7
[chanl2] => 33.8
[chanl3] =>
)
[3] => Array
(
[dateTime] => 2011-10-18 00:29:00
[chanl1] => 20.6
[chanl2] => 33.9
[chanl3] =>
)
I want to sort the above array based on the [dateTime], the final output should be:
我想根据[dateTime]对上面的数组进行排序,最终输出应该是:
Array
(
[0] => Array
(
[dateTime] => 2011-10-18 0:0:00
[chanl1] => 20.7
[chanl2] => 45.4
[chanl3] =>
)
[1] => Array
(
[dateTime] => 2011-10-18 00:14:00
[chanl1] => 20.7
[chanl2] => 33.8
[chanl3] =>
)
[2] => Array
(
[dateTime] => 2011-10-18 0:15:00
[chanl1] => 20.7
[chanl2] => 45.4
[chanl3] =>
)
[3] => Array
(
[dateTime] => 2011-10-18 00:29:00
[chanl1] => 20.6
[chanl2] => 33.9
[chanl3] =>
)
Is there anyone know how to do it? Thanks!
有没有人知道怎么做?谢谢!
回答by Crozin
Use usort()
functionwith custom comparator:
将usort()
函数与自定义比较器一起使用:
$arr = array(...);
usort($arr, function($a, $b) {
$ad = new DateTime($a['dateTime']);
$bd = new DateTime($b['dateTime']);
if ($ad == $bd) {
return 0;
}
return $ad < $bd ? -1 : 1;
});
DateTimeclass has overloaded comparison operators (<
, >
, ==
).
DateTime类具有重载的比较运算符 ( <
, >
, ==
)。
回答by Expedito
For performance, this method using array_multisortis very efficient:
对于性能,这种使用array_multisort 的方法非常有效:
$ord = array();
foreach ($array as $key => $value){
$ord[] = strtotime($value['dateTime']);
}
array_multisort($ord, SORT_ASC, $array);
print_r($array);
回答by Clive
Using uasort()
with a custom sort callback should do it:
使用uasort()
带有自定义排序回调应该这样做:
function cmp($a, $b) {
if ($a['dateTime'] == $b['dateTime']) {
return 0;
}
return ($a['dateTime'] < $b['dateTime']) ? -1 : 1;
}
uasort($arr, 'cmp');
uasort()
preserves your array's keys, you can use usort()
instead if you don't need that to happen.
uasort()
保留数组的键,usort()
如果不需要,可以改用它。
回答by Clive
Use array_multisort :
使用 array_multisort :
<?php
$myarray=array(
0 => array
(
'dateTime' => '2011-10-18 00:0:00',
'chanl1' => '20.7',
'chanl2' => '45.4',
'chanl3' => '',
),
1 => array
(
'dateTime' => '2011-10-18 00:15:00',
'chanl1' => '20.7',
'chanl2' => '45.4',
'chanl3' => '',
),
2 => array
(
'dateTime' => '2011-10-18 00:14:00',
'chanl1' => '20.7',
'chanl2' => '33.8',
'chanl3' => '',
),
3 => array
(
'dateTime' => '2011-10-18 00:29:00',
'chanl1' => '20.6',
'chanl2' => '33.9',
'chanl3' => ''
));
foreach($myarray as $c=>$key) {
$dateTime[] = $key['dateTime'];
$chanl1[] = $key['chanl1'];
$chanl2[] = $key['chanl2'];
$chanl3[] = $key['chanl3'];
}
array_multisort($dateTime,SORT_ASC,SORT_STRING,$myarray);
print_r($myarray);
回答by Mani
For php array and object this example is helpful for sorting...
对于 php 数组和对象,此示例有助于排序...
For datetime descending:
对于日期时间降序:
usort($response_data['callback'], function($a, $b) {
return strcasecmp(strtotime($b->view_date), strtotime($a->view_date));
});
For datetime ascending:
对于日期时间升序:
usort($response_data['callback'], function($a, $b) {
return strtotime($a->view_date) - strtotime($b->view_date);
});
回答by Stefan P?ltl
Simple sorting for DateTime objects with the spaceship operator:
使用宇宙飞船运算符对 DateTime 对象进行简单排序:
$list = [
new DateTime('yesterday'),
new DateTime('today'),
new DateTime('1 week ago'),
];
uasort($list, function ($a, $b) {
return $a <=> $b;
});
var_dump($list);