I'm trying to make a simple website that uses the coinmarketcap api to track current cryptocurrency prices, and the code below works but it doesn't account for when currencies overtake each other.
<?php
$tick = file_get_contents('https://api.coinmarketcap.com/v1/ticker/');
$url = $tick;
echo $url;
$json = file_get_contents($url);
$data = json_decode($tick, TRUE); //decodes in associative array
$ethusd = $data[1]['price_usd'];
echo $ethusd;
?>
And this is the json being decoded
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "16474.4",
"price_btc": "1.0",
"24h_volume_usd": "13440600000.0",
"market_cap_usd": "275760451140",
"available_supply": "16738725.0",
"total_supply": "16738725.0",
"max_supply": "21000000.0",
"percent_change_1h": "-2.32",
"percent_change_24h": "-5.68",
"percent_change_7d": "24.41",
"last_updated": "1513187054"
},
{
"id": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_usd": "684.996",
"price_btc": "0.04138",
"24h_volume_usd": "4731760000.0",
"market_cap_usd": "65976663404.0",
"available_supply": "96316859.0",
"total_supply": "96316859.0",
"max_supply": null,
"percent_change_1h": "-5.62",
"percent_change_24h": "12.04",
"percent_change_7d": "56.0",
"last_updated": "1513187055"
},
{
"id": "bitcoin-cash",
"name": "Bitcoin Cash",
"symbol": "BCH",
"rank": "3",
"price_usd": "1594.38",
"price_btc": "0.0963151",
"24h_volume_usd": "1286400000.0",
"market_cap_usd": "26871042768.0",
"available_supply": "16853600.0",
"total_supply": "16853600.0",
"max_supply": "21000000.0",
"percent_change_1h": "-3.9",
"percent_change_24h": "1.8",
"percent_change_7d": "8.88",
"last_updated": "1513187076"
},
etc.
Since ether is the second highest currency in the json it works for now, but if it moves it wouldn't. So is there a way for me to do something like $data[something that specifies ether]['price_usd'] instead? Apologies in advance for any PHP ignorance, I know very little about PHP/arrays/etc and just started working with it a few days ago.