D
D
dimavfox2019-07-10 16:12:01
PHP
dimavfox, 2019-07-10 16:12:01

How to get data from JSON string?

<?php 
  $url = file_get_contents('https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH,BCH&tsyms=BTC,USD&api_key=MY-API-KEY');

  $json = json_decode($url, true);
  var_dump($json);
  echo $json[0];
?>

Result var_dump:
array(3) { ["BTC"]=> array(2) { ["BTC"]=> int(1) ["USD"]=> float(13035.95) } ["ETH"]=> array(2) { ["BTC"]=> float(0.02365) ["USD"]=> float(308.28) } ["BCH"]=> array(2) { ["BTC"]=> float(0.03184 ) ["USD"]=> float(414.79) } }
Result echo $url;
{"BTC":{"BTC":1,"USD":13036.56},"ETH":{"BTC":0.02365,"USD":308.27},"BCH":{"BTC":0.03184,"USD ":414.81}}
How can I get the values ​​from here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Panteleev, 2019-07-10
@dimavfox

Well $json[0], it doesn't work, because you don't have a 0 index,
but what are the problems with data access?

<?php

$url = file_get_contents('https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH,BCH&tsyms=BTC,USD&api_key=MY-API-KEY');

$json = json_decode($url, true);
echo 'Eth to usd: ' . $json['ETH']['USD'];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question