![](/img/trans.png)
[英]How to convert json to php array and only pull specific value from json
[英]Pull specific value from JSON with PHP
我試圖從以下JSON中提取價格,但似乎無法弄清楚如何引用實際值:
{"cards": [ { "high": "0.73", "volume": 1, "percent_change": "-2.67", "name": "Lightning Bolt", "url": "http://blacklotusproject.com/cards/Revised+Edition/Lightning+Bolt/", "price": "0.73", "set_code": "3ED", "average": "0.73", "change": "-0.02", "low": "0.73"}], "currency": "USD" }
到目前為止,我已經有了這個代碼,它進入了卡陣列,但我不確定如何進一步 - 我嘗試過的每一次嘗試都返回null。
$json = file_get_contents($url); $data = json_decode($json, TRUE);
echo var_dump($data[cards]);
有人能說清楚我需要做什么嗎?
$data['cards']
有另一個數組。 您將需要使用索引0
訪問此數組。 例如, $data['cards'][0]['high']
等等。
$data['cards']
本身就是一個數組,所以你可以這樣做:
foreach ($data['cards'] AS $carditem) {
echo $carditem['high'];
...
}
獲取該數組中的所有項目,或者如果您只想要第一項$data['cards'][0]['...']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.