Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Cannot use object of type stdClass as array?

I get a unusual error utilizing json_decode(). It unravels accurately the data (I saw it utilizing print_r), however when I attempt to access information inside the array I get

Fatal error: Cannot use object of type stdClass as array in
C:\Users\Dail\software\abs.php on line 108


I simply attempted to do: $result['context'] where $result has the data returned by json_decode()

How might I peruse values inside this array?
by

2 Answers

akshay1995
Use the second parameter of json_decode to make it return an array:

$result = json_decode($data, true);
sandhya6gczb
The function json_decode() returns an object by default.

You can access the data like this:

var_dump($result->context);

If you have identifiers like from-date (the hyphen would cause a PHP error when using the above method) you have to write:

var_dump($result->{'from-date'});

If you want an array you can do something like this:

$result = json_decode($json, true);

Or cast the object to an array:

$result = (array) json_decode($json);

Login / Signup to Answer the Question.