来自多维json数组的{php}变量

时间:2017-08-17 17:02:41

标签: php arrays json

我想将rate_float作为json的变量:

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

我从j:https://api.coindesk.com/v1/bpi/currentprice/IDR.json

获取json

我尝试输入以下编码,但我从IDR获得了所有值,

我只想要一个rate_float

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

谢谢,请帮忙

1 个答案:

答案 0 :(得分:0)

你几乎就在那里,只是一个小小的变化: -

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

输出: - https://eval.in/846717

如果您只想IDR rate_float,请执行以下操作: -

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

输出: - https://eval.in/846725