-1

<?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>

Hello the problem is the json_decode its not printing object price_usd anyone know how to print just the object price_usd

Thanks.

Oliver Maksimovic
  • 3,204
  • 3
  • 28
  • 44
john doe
  • 1
  • 1
  • Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – mickmackusa Dec 21 '17 at 21:12

1 Answers1

0

If you do var_dump($array) you can see the exact structure of it. Your current way of trying to get it doesn't make much sense.

The JSON from the API is this:

[
    {
        "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"
    }
]

...and after converting it to an assoc array (by passing true to json_decode() as its 2nd param, fetching the rate should be done as:

$array[0]['price_usd']

Oliver Maksimovic
  • 3,204
  • 3
  • 28
  • 44