4

I'm trying to read the returned json below but i keep getting errors.

Cannot use object of type stdClass as array

I'm fetching the 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

Var Dump 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) } } 
3
  • @nerdyDev please avoid providing solutions as comments. Better still, vote to close this page as a duplicate. Commented Jan 15, 2018 at 6:46
  • 1
    Possible duplicate of How to parse json response from CURL Commented Jan 15, 2018 at 6:49
  • ok will remove my comment. Commented Jan 15, 2018 at 6:50

2 Answers 2

5

Decode as Associative Array

For decoding JSON as an array, you have to pass assoc parameter to the json_decode function.

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

Refer PHP Docs for more information regarding the function.

Example Code

json_decode($data, true);
Sign up to request clarification or add additional context in comments.

1 Comment

If this is the solution, then this question is an under-researched, super-mega-duplicate on StackOverflow.
0

There is a second parameter to json_decode which allows you to parse json data as associative arrays. try:

json_decode($data, true);

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.