看了几个关于此的问题,但似乎无法做任何事情。我想订购" pct"从最高百分比到最低百分比。以下是我的代码......
<?
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = json_decode(curl_exec($ch));
usort($data, function ($a, $b) {
return $a->pct - $b->pct;
});
curl_close($ch);
foreach ($data->races as $slug => $race) {
$p = $race->pctCount;
$pRetty = prettyNum($p);
$multiball = ($race->pos > 1) ? '<a class="multiRace"><em>Top '.$race->pos.' advance</em></a>' : false;
echo '<div class="race"><h3>'.$race->race.'</h3>'.$multiball.'<table class="'.$slug.' race">';
if ($race->pctCount != 0) { echo '<div class="pace" data-pct="'.round($p,3).'"><a class="progbar"></a>'.$pRetty.' counted</div>'; }
foreach ($race->candidates as $cand) {
echo '<tr class="cand '.$cand->party.'"><td class="bar"><a class="statsBar"></a></td><td>'.$cand->name.' ('.$cand->party.')'.'</td><td class="results">'.prettyNum($cand->pct).'</td></tr>';
}
echo '</table></div>';
}
function prettyNum($num) {
return (round($num,3)*100).'%';
}
?>
</body>
</html>
答案 0 :(得分:0)
在usort
回调中,您将返回减法结果,而不是比较结果。你做过这样的事情:
usort($data, function($a, $b) {
return $a->pct < $b-pct; // or any other comparing like equals, greater, etc.
});