Cannot use object of type stdClass as array?

Viewed 909766

I get a strange error using json_decode(). It decode correctly the data (I saw it using print_r), but when I try to access to info 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 only tried to do: $result['context'] where $result has the data returned by json_decode()

How can I read values inside this array?

16 Answers

Try something like this one!

Instead of getting the context like:(this works for getting array index's)

$result['context']

try (this work for getting objects)

$result->context

Other Example is: (if $result has multiple data values)

Array
(
    [0] => stdClass Object
        (
            [id] => 15
            [name] => 1 Pc Meal
            [context] => 5
            [restaurant_id] => 2
            [items] => 
            [details] => 1 Thigh (or 2 Drums) along with Taters
            [nutrition_fact] => {"":""}
            [servings] => menu
            [availability] => 1
            [has_discount] => {"menu":0}
            [price] => {"menu":"8.03"}
            [discounted_price] => {"menu":""}
            [thumbnail] => YPenWSkFZm2BrJT4637o.jpg
            [slug] => 1-pc-meal
            [created_at] => 1612290600
            [updated_at] => 1612463400
        )

)

Then try this:

foreach($result as $results)
{
      $results->context;
}

To get an array as result from a json string you should set second param as boolean true.

$result = json_decode($json_string, true);
$context = $result['context'];

Otherwise $result will be an std object. but you can access values as object.

  $result = json_decode($json_string);
 $context = $result->context;

Sometimes when working with API you simply want to keep an object an object. To access the object that has nested objects you could do the following:

We will assume when you print_r the object you might see this:

print_r($response);

stdClass object
(
    [status] => success
    [message] => Some message from the data
    [0] => stdClass object
        (
            [first] => Robert
            [last] => Saylor
            [title] => Symfony Developer
        )
    [1] => stdClass object
        (
            [country] => USA
        )
)

To access the first part of the object:

print $response->{'status'};

And that would output "success"

Now let's key the other parts:

$first = $response->{0}->{'first'};
print "First name: {$first}<br>";

The expected output would be "Robert" with a line break.

You can also re-assign part of the object to another object.

$contact = $response->{0};
print "First Name: " . $contact->{'first'} . "<br>";

The expected output would be "Robert" with a line break.

To access the next key "1" the process is the same.

print "Country: " . $response->{1}->{'country'} . "<br>";

The expected output would be "USA"

Hopefully this will help you understand objects and why we want to keep an object an object. You should not need to convert an object to an array to access its properties.

Related