我正在使用coinmarketcap api来获得前100种货币。默认情况下,响应按等级排序,但我希望响应按
我要按价格降序显示它们。
$data = json_decode(file_get_contents('https://api.coinmarketcap.com/v1/ticker/'), true);
foreach ($data as $key => $value) {
$id = $value["id"];
$name = $value["name"];
$price_usd = $value["price_usd"];
$percent_change_24h = $value['percent_change_24h'];
$percent_change_7d = $value['percent_change_7d'];
echo "<tr>";
echo "<td>". $id . "<span>" . $name . "/ USD</span></td>";
echo "<td>". $price_usd ."</td>";
echo "<td><i class = 'fa fa-caret-up' aria-hidden = 'true'>". $percent_change_24h . "<span> " . $percent_change_7d . "</span></i></td>";
echo "</tr>";
}
答案 0 :(得分:1)
array-multisort怎么样?您可以这样做:
$data = json_decode(file_get_contents('https://api.coinmarketcap.com/v1/ticker/'), true);
array_multisort(array_column($data, "price_usd"), SORT_DESC, $data);
或者您可以将price_usd
更改为所需的任何字段,然后将$data
从最高到最低排序,然后可以在其上运行for-loop