2017-08-04 116 views
-2

請幫我把JSON數據 「price_usd」JSON幫助獲得內容

https://api.coinmarketcap.com/v1/ticker/bitcoin-cash/

[ 
{ 
    "id": "bitcoin-cash", 
    "name": "Bitcoin Cash", 
    "symbol": "BCH", 
    "rank": "4", 
    "price_usd": "338.53", 
    "price_btc": "0.121547", 
    "24h_volume_usd": "189098000.0", 
    "market_cap_usd": "5579888431.0", 
    "available_supply": "16482700.0", 
    "total_supply": "16482700.0", 
    "percent_change_1h": "-4.29", 
    "percent_change_24h": "-23.79", 
    "percent_change_7d": "0.43", 
    "last_updated": "1501831807" 
} ] 


<?php 

$json = file_get_contents('https://api.coinmarketcap.com/v1/ticker/bitcoin-cash/'); 
$data = json_decode($json, TRUE); 

$price_usd = $data["price_usd"]; 

echo $price_usd; 

?> 

PHP腳本沒有什麼

+7

的[?我如何從JSON中提取數據與PHP(可能的複製https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from -json-with-php) –

+0

查看數組make:print_r($ data); –

+0

WORK:echo $ data [0] ['price_usd']; –

回答

0

$數據對象的數組,所以你不能得到像數據那。 試試這個代碼:

$url = "https://api.coinmarketcap.com/v1/ticker/bitcoin-cash/"; 
$dataRaw = file_get_contents($url); 
$data = json_decode($dataRaw); 
$coin = $data[0]; 
foreach ($data as $key => &$value) { 
    $value = (array)$value;//convert Object to array 
    echo $value['price_usd']; 
} 
+0

請不要回答重複的問題。 – mickmackusa