[英]sort Json data in php ? Different specific data?
这是webservice中的json数据,我需要按照最新的排序方式对此类信息进行排序。2017..bla-2015..bla -2014,也许将值“ 4”或标题2015 blabla ....在PHP中排序脚本,我该怎么办?
"2015-09-11 09:18:11.023151": {
"1": "4422",
"2": "asd",
"3": "aaa",
"4": "2015-09-11 09:18:11.023151"
},
"2014-10-29 13:33:14.325827": {
"1": "5852",
"2": "shfe",
"3": "sds",
"4": "2014-10-29 13:33:14.325827"
},
"2017-11-10 09:18:11.315102": {
"1": "2323",
"2": "sfd",
"3": "sdf",
"4": "2017-11-10 09:18:11.315102"
}
我尝试:
$example= json_decode($result, true);
usort($example, function($a, $b) {
return $a->{"4"} > $b->{"4"} ? -1 : 1; });
echo json_encode($example);
我们该怎么做?
请尝试一下。
$result = '{"2015-09-11 09:18:11.023151":{"1":"4422","2":"asd","3":"aaa","4":"2015-09-11 09:18:11.023151"},"2014-10-29 13:33:14.325827":{"1":"5852","2":"shfe","3":"sds","4":"2014-10-29 13:33:14.325827"},"2017-11-10 09:18:11.315102":{"1":"2323","2":"sfd","3":"sdf","4":"2017-11-10 09:18:11.315102"}}';
$example= json_decode($result, true);
usort($example, function($a, $b) { return $a["4"] > $b["4"] ? -1 : 1; });
echo json_encode($example);
我想我解决了你的问题。 我首先将json转换为数组,就像使用$array = json_decode($json, true);
接下来而不是使用usort()
我使用asort()
。
Asort将您的数组从高到低排序并按日期运行(以我为例)
http://php.net/manual/en/function.asort.php
你是这个意思吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.