使用 usort 对 json 输出进行排序

Using usort to sort json output

我有以下 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);

它应该按预期工作。