首先,有问题的json数据不是数组而是对象!
可视化任何一段 json 数据的简单方法是这样做
<?php
$j = '{"time":{"updated":"Oct 26, 2015 12:54:00 UTC","updatedISO":"2015-10-26T12:54:00+00:00","updateduk":"Oct 26, 2015 at 12:54 GMT"},"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":"283.9203","description":"United States Dollar","rate_float":283.9203},"GBP":{"code":"GBP","rate":"185.0732","description":"British Pound Sterling","rate_float":185.0732}}}';
print_r( json_decode($j) );
在这种情况下会产生:-
stdClass Object
(
[time] => stdClass Object
(
[updated] => Oct 26, 2015 12:54:00 UTC
[updatedISO] => 2015-10-26T12:54:00+00:00
[updateduk] => Oct 26, 2015 at 12:54 GMT
)
[disclaimer] => This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchang
erates.org
[bpi] => stdClass Object
(
[USD] => stdClass Object
(
[code] => USD
[rate] => 283.9203
[description] => United States Dollar
[rate_float] => 283.9203
)
[GBP] => stdClass Object
(
[code] => GBP
[rate] => 185.0732
[description] => British Pound Sterling
[rate_float] => 185.0732
)
)
)
这很简单地告诉您a)它是一个对象,其属性也是对象等,以及b)如何引用对象中的任何属性。
所以要得到你想要的汇率,我会这样做:-
<?php
$j = '{"time":{"updated":"Oct 26, 2015 12:54:00 UTC","updatedISO":"2015-10-26T12:54:00+00:00","updateduk":"Oct 26, 2015 at 12:54 GMT"},"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":"283.9203","description":"United States Dollar","rate_float":283.9203},"GBP":{"code":"GBP","rate":"185.0732","description":"British Pound Sterling","rate_float":185.0732}}}';
$bitExchange = json_decode($j);
// the GBP exchange rate will be
echo $bitExchange->bpi->GBP->rate;
// or if you prefer
echo $bitExchange->bpi->GBP->rate_float;