在echo中打印$ data?

时间:2015-08-19 11:14:10

标签: php json

我的代码:

$url = "https://apilayer.net/api/live?currencies=USD,EUR,GBP&source=USD&format=1";
$json = file_get_contents($url);
$thedata = json_decode($json, true);

echo $thedata['quotes']['USDEUR'];

这是我得到的JSON:

"quotes":{
  "USDUSD":1,
  "USDEUR":0.90629,
  "USDGBP":0.63802
}

所以现在我有一个数据库和列名称货币。所以我想把货币表变成php。 和货币=喜欢

id  -  currency
1   -  USD
2   -  GBP
3   -  USD
AND MORE ...

所以我这样做:

$url = "https://apilayer.net/api/live?currencies=USD,EUR,GBP&source=$data[currency]&format=1";

但是在echo数据中我要打印

echo $thedata['quotes']['USDEUR'];

但我想更改USD TO $ data [货币]

像这样:

echo $thedata['quotes']['$data[currency]EUR'];

2 个答案:

答案 0 :(得分:0)

尝试:

echo $thedata['quotes']["$data[currency]EUR"];

答案 1 :(得分:0)

试试这个:

echo $thedata['quotes']['$data[currency]'].$thedata['quotes']['EUR'];

。将两个字符串组合成一个回声,因此您应该得到所需的输出。

相关问题