1

I want to take rate_float as a variable from json following :

{
"time": {
    "updated": "Aug 17, 2017 14:21:00 UTC",
    "updatedISO": "2017-08-17T14:21:00+00:00",
    "updateduk": "Aug 17, 2017 at 15:21 BST"
},
"disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
"bpi": {
    "USD": {
        "code": "USD",
        "rate": "4,504.1325",
        "description": "United States Dollar",
        "rate_float": 4504.1325
    },
    "IDR": {
        "code": "IDR",
        "rate": "60,204,487.0613",
        "description": "Indonesian Rupiah",
        "rate_float": 60204487.0613
    }
  }
}

I take json from : https://api.coindesk.com/v1/bpi/currentprice/IDR.json

I tried by typing the following coding but for which I got all values from IDR,

I just wanted a rate_float only

$url_json="http://api.coindesk.com/v1/bpi/currentprice/IDR.json";
$result=file_get_contents($url_json);
$array=json_decode($result, true);

foreach($array['bpi']['IDR'] as $currency => $data) {
  echo $data."<br>"; 
}

Thank you, please help

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • 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) – Don't Panic Aug 17 '17 at 17:37

1 Answers1

0

You are almost there, just a small change:-

foreach($array['bpi'] as $currency => $data) {
   echo $data['rate_float']."<br>"; 
}

Output:-https://eval.in/846717

If you want only IDR rate_float then do like below:-

foreach($array['bpi'] as $key=> $data) {
   if($key == 'IDR'){
     echo $data['rate_float']."<br>";
   }
}

Output:- https://eval.in/846725

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98