PHP - Read Json and parse the array

I am trying to read the returned json below, but I keep getting errors.

You cannot use an object of type stdClass as an array

I retrieve json via curl and then json_decode ($ data);

foreach($array as $a)
{
   switch($a)
   {
       case"BTC":
         //do something
       case"ETH":
         //do something


   }

}

URL:

https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,LTC,XMR,XRP,DASH,ZEC&tsyms=USD

Varna Reset Results:

object(stdClass)#405 (6) { ["BTC"]=> object(stdClass)#404 (1) { ["USD"]=> float(13571.4) } ["LTC"]=> object(stdClass)#406 (1) { ["USD"]=> float(235.57) } ["XMR"]=> object(stdClass)#407 (1) { ["USD"]=> float(399.11) } ["XRP"]=> object(stdClass)#408 (1) { ["USD"]=> float(1.83) } ["DASH"]=> object(stdClass)#409 (1) { ["USD"]=> float(1000.25) } ["ZEC"]=> object(stdClass)#410 (1) { ["USD"]=> float(658.29) } } 
+4
source share
2 answers

Decode as an associative array

To decode JSON as an array, you need to pass the assoc parameter to the function json_decode.

When the assoc parameter is TRUE, the returned objects will be converted to associative arrays.

. PHP.

json_decode($data, true);
+5

json_decode, json . :

json_decode($data, true);
0

Source: https://habr.com/ru/post/1692187/


All Articles