[英]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.