根据php中的日期时间对数组进行排序
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] =>
)
我想根据[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] =>
)
有谁知道该怎么做吗?谢谢!
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:
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将
usort()
函数与自定义比较器一起使用:< a href="http://www.php.net/manual/en/class.datetime.php" rel="noreferrer">DateTime 类重载了比较运算符 (
<
,>
, <代码>==)。Use
usort()
function with custom comparator:DateTime class has overloaded comparison operators (
<
,>
,==
).就性能而言,这种使用 array_multisort 的方法非常高效:
For performance, this method using array_multisort is very efficient:
使用 spaceship 运算符对 DateTime 对象进行简单排序:
Simple sorting for DateTime objects with the spaceship operator:
使用
uasort()
和自定义排序回调应该可以做到这一点:uasort()
保留数组的键,您可以使用usort()
相反,如果您不需要这种情况发生。Using
uasort()
with a custom sort callback should do it:uasort()
preserves your array's keys, you can useusort()
instead if you don't need that to happen.对于 php 数组和对象,此示例有助于排序...
对于日期时间降序:
对于日期时间升序:
For php array and object this example is helpful for sorting...
For datetime descending:
For datetime ascending:
使用 array_multisort :
Use array_multisort :