简体   繁体   English

在PHP中访问JSON对象,数组对象中的值

[英]Accessing values in a JSON object, array object in PHP

When looking at the following JSON string: 查看以下JSON字符串时:

$string = '{"data":{"exchange_rates":{"2":[{"cryptoCurrency":"BTC","rateForCashCurrency":{"EUR":1273.261000,"USD":1358.5694870000000000}}],"4":[{"cryptoCurrency":"BTC","rateForCashCurrency":{"EUR":1033.839000,"USD":1103.1062130000000000}}]}},"message":null,"status":"ok"}';

I can access the values of the status key in php using: 我可以使用以下命令在php中访问状态键的值:

 $rates_o=json_decode($string);
 echo $rates_o->status; (using the example string above result is "ok")

Where I am completely lost is how to access the Label / values in the exchange_rates "4" EUR and USD rates in the JSON above. 我完全迷失的是如何在上面的JSON中访问exchange_rates“ 4” EUR和USD汇率中的Label /值。 I think this is caused because the object is in an array in an object setup of the response? 我认为这是由于对象位于响应的对象设置中的数组中引起的吗?

I tried: 我试过了:

print_r($rates_o->data->exchange_rates->4[0]); 

but get a parse error in PHP: 但是在PHP中得到一个解析错误:

PHP Parse error:  syntax error, unexpected '4' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

What is the easiest way to loop through the different currency entries and values (USD EUR, etc) in the section "4" of the JSON above? 遍历上面JSON的“ 4”部分中的不同货币条目和值(USD EUR等)的最简单方法是什么?

This is my first post, I tried finding similar examples and there are many but can't find a solution to this 'nested' problem as none of the examples had any clues regarding this. 这是我的第一篇文章,我尝试查找类似的示例,但是有很多但找不到解决此“嵌套”问题的解决方案,因为这些示例都没有与此相关的任何线索。

json_decode takes a second parameter to decode your json as an array, json_decode使用第二个参数将json解码为数组,

then you would be able to access it normally as follows: 那么您将可以正常访问它,如下所示:

$rates_o=json_decode($string, true);
print_r($ar['data']['exchange_rates'][4][0]);

When accessing an object property that its name is an number you must put it between {}. 访问名称为数字的对象属性时,必须将其放在{}之间。 So: 所以:

print_r($data->data->exchange_rates->{4}[0]);

Here you go: 干得好:

$string = '{"data":{"exchange_rates":{"2":[{"cryptoCurrency":"BTC","rateForCashCurrency":{"EUR":1273.261000,"USD":1358.5694870000000000}}],"4":[{"cryptoCurrency":"BTC","rateForCashCurrency":{"EUR":1033.839000,"USD":1103.1062130000000000}}]}},"message":null,"status":"ok"}';
$rates_o = json_decode($string);
var_dump($rates_o->data->exchange_rates->{4}[0]);

-> ->

object(stdClass)#4 (2) {
  ["cryptoCurrency"]=>
  string(3) "BTC"
  ["rateForCashCurrency"]=>
  object(stdClass)#5 (2) {
    ["EUR"]=>
    float(1033.839)
    ["USD"]=>
    float(1103.106213)
  }
}

What is the easiest way to loop through the different currency entries and values (USD EUR, etc) in the section "4" of the JSON above? 遍历上面JSON的“ 4”部分中的不同货币条目和值(USD EUR等)的最简单方法是什么?

Could loop through exchange rates like so, 可以像这样遍历汇率,

$x=array('EUR','USD');
foreach($x as $v)
    echo $rates_o->data->exchange_rates->{4}[0]->rateForCashCurrency->$v . PHP_EOL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM