【发布时间】:2015-12-10 09:10:53
【问题描述】:
我有一种情况,我需要按日期对 JSON 对象进行排序。我在网上搜索了解决方案,一切都指向 PHP 的 usort 函数的方向,但 all examples 有一个键/值对可以排序。
这是我加载提要的方式:
$ret = file_get_contents($url);
$res = json_encode($ret);
以下 JSON 的结果
{
"2015-12-14":{
"direction":"S",
"snowfall":0.0,
[..]
},
"2015-12-15":{
"direction":"S",
"snowfall":3.0,
[..]
},
"2015-12-12":{
"direction":"SE",
"snowfall":0.0,
[..]
},
"2015-12-13":{
"direction":"S",
"snowfall":0.0,
[..]
},
"2015-12-10":{
"direction":"E",
"snowfall":0.0,
[..]
},
"2015-12-11":{
"direction":"S",
"snowfall":0.0,
[..]
}
}
如您所见,数据未按日期正确排序,但日期值是关键,所以我如何按日期对对象进行排序(2015-12-10、2015-12-11、2015-12- 12、2015-12-13、2015-12-14、2015-12-15)?
【问题讨论】: