douzhangbao2187 2017-06-08 19:51
浏览 35
已采纳

从公共参数排序的api获取值

How would one go about listing all the values in this api in a html table ordered by their "position24" value?

I know how to call each individual value, but I'm not sure of how to automatically load them in that order.

http://www.coincap.io/front

  • 写回答

1条回答 默认 最新

  • douwen5924 2017-06-08 20:56
    关注

    You can call your API, decode the JSON, sort it by this key and return it in a table. The following lines are a base for your solution.

    function compare($a, $b) {
        return intval($a->position24) - intval($b->position24);
    }
    
    $json = file_get_contents('http://www.coincap.io/front');
    $data = json_decode($json);
    usort($data, 'compare');
    // display $data in a table
    

    If you use classes and the compare function (compare) is in the same class as the usort-function-call you should use:

    usort($data, array($this, 'compare'));
    

    UPDATE:

    You can echo out your table by calling the attribute like this:

    foreach ($data as $row) {
        echo $row->long;
        echo $row->position24;
    }
    

    Because there are so many values, I let it up to, which one you want to display. If you need help how to build a table you find more information here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?