1

I need to extract data from a crypto API and I am using this PHP code

  $url = 'https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH&tsyms=USD,EUR';
  $data = file_get_contents($url);
  $priceInfo = json_decode($data);

foreach ($priceInfo as $key ){
foreach ($key as $vala => $valb ){
    echo "<br> $vala--> $valb " ;
}
} 

which is returning this

USD--> 49463.82 
EUR--> 40802.62 
USD--> 1630.65 
EUR--> 1345.66

How should I change the code to show this ?

BTC USD--> 49463.82 
BTC EUR--> 40802.62 
ETH USD--> 1630.65 
ETH EUR--> 1345.66

The array $priceInfo return this

stdClass Object ( [BTC] => stdClass Object ( [USD] => 49432.16 [EUR] => 40773.08 ) [ETH] => stdClass Object ( [USD] => 1628.21 [EUR] => 1343.67 ) )

3 Answers 3

3

You can access the key of the first loop.

foreach ($priceInfo as $crypto => $obj ){
foreach ($obj as $cur => $val ){
    echo "<br>$crypto $cur--> $val " ;
}
}
Sign up to request clarification or add additional context in comments.

Comments

1

you can use $key's value as below

foreach ($priceInfo as $key ){
  foreach ($key as $vala => $valb ){
      echo "<br>$key $vala--> $valb " ;
       }
   } 

Comments

1
$priceInfo = json_decode($data, true);
var_dump($priceInfo);

later do with this array wha you need

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.