我在json下有outout。有人可以建议如何根据“ no_count”列对此进行排序吗?
$arr= {"UserHeader":[
{"id":"154", "no_count":15},
{"id":"155", "no_count":11},
{"id":"158", "no_count":13},
{"id":"159", "no_count":31},
{"id":"164", "no_count":11}
]}
我曾经用过USORT,但没有运气。该代码无法正常工作,没有排序就给我相同的数组。
usort($arr, function($a, $b) { //Sort the array using a user defined function
return $a->no_count > $b->no_count ? -1 : 1; //Compare the scores
});
print_r($arr);
可能是一个小的格式化错误。
谢谢
我做了一些更正:
$arr = json_decode('{"UserHeader":[
{"id":"154", "no_count":15},
{"id":"155", "no_count":11},
{"id":"158", "no_count":13},
{"id":"159", "no_count":31},
{"id":"164", "no_count":11}
]}');
usort($arr->UserHeader, function ($a, $b) { //Sort the array using a user defined function
return $a->no_count > $b->no_count ? -1 : 1; //Compare the scores
});
print_r($arr);
它应该按预期工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句