json_decode不打印对象price_usd

时间:2017-12-21 19:45:30

标签: json decode

<?php

      
	  $url = file_get_contents("https://api.coinmarketcap.com/v1/ticker/bitcoin-cash/");
	  $array = json_decode($url,TRUE);
	  // print_r($array);
      $rate['price_usd'] =  $array->{"bitcoin-cash"}->{"price_usd"};
      $rate = $rate['price_usd'];

?>



      <center><font color="black">Rate: 1 <font color="green">BCH/BitCoin Cash</font> = <?=$rate?> USD</font></center>

你好问题是json_decode它不是打印对象price_usd 任何人都知道如何只打印对象price_usd

感谢。

1 个答案:

答案 0 :(得分:0)

如果你var_dump($array),你可以看到它的确切结构。你目前试图获得它的方式没有多大意义。

API中的JSON是:

[
    {
        "id": "bitcoin-cash", 
        "name": "Bitcoin Cash", 
        "symbol": "BCH", 
        "rank": "3", 
        "price_usd": "3205.57", 
        "price_btc": "0.208515", 
        "24h_volume_usd": "4509330000.0", 
        "market_cap_usd": "54073197615.0", 
        "available_supply": "16868513.0", 
        "total_supply": "16868513.0", 
        "max_supply": "21000000.0", 
        "percent_change_1h": "-0.83", 
        "percent_change_24h": "-17.54", 
        "percent_change_7d": "71.22", 
        "last_updated": "1513885472"
    }
]

...并在将其转换为关联数组后(通过将true传递给json_decode()作为其第二个参数,获取速率应该如下:

$array[0]['price_usd']