如何按照硬币市场api v1按价格对美元进行排序?

问题描述 投票:2回答:1

我正在使用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>";  
 }  
php arrays api
1个回答
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更改为您需要的任何字段,并且nop $data将从最高到最低排序,您可以在其上运行for-loop

© www.soinside.com 2019 - 2024. All rights reserved.