简体   繁体   English

如何按 coinmarketcap api v1 按美元价格对响应进行排序?

[英]How to sort response by coinmarketcap api v1 by price in usd?

I am using coinmarketcap api to get top 100 cyptocurrencies.我正在使用 coinmarketcap api 来获取前 100 种加密货币。 By default, the response is sorted by rank, but i want the response to be sorted by price .默认情况下,响应按排名排序,但我希望响应按price排序。

I want to display them sorted by price in descending order.我想按价格降序显示它们。

$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>";  
 }  

How about array-multisort ?数组多重排序怎么 you can do it like:你可以这样做:

$data = json_decode(file_get_contents('https://api.coinmarketcap.com/v1/ticker/'), true);
array_multisort(array_column($data, "price_usd"), SORT_DESC, $data);

Or you can change price_usd to any field you need and nop $data will be sorted form highest to lowest and you can run your for-loop on it或者您可以将price_usd更改为您需要的任何字段,nop $data将从高到低排序,您可以在其上运行for-loop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM