Steve Kim
Steve Kim

Reputation: 5591

PHP Cannot use object of type stdClass as array error

I have an array of objects with multiple keys in php:

Array
(
[0] => stdClass Object
    (
        [ID] => 4983
        [post_id] => 56357
        [date] => 2016-06-04 23:45:28          
    )

[1] => stdClass Object
    (
        [ID] => 4982
        [post_id] => 56241
        [date] => 2016-06-04 22:58:27           
    )
 )

Then I am using the following to change the date format:

foreach($results as &$row) {    
   $row['date'] = ago($row['date']);
}

However I keep getting Cannot use object of type stdClass as array error.

Any suggestions to why it is occurring?

Upvotes: 0

Views: 74

Answers (1)

Jarvis
Jarvis

Reputation: 679

You need to access it as an object. Its an array of objects... Your var_dump showed that. Here is how your for loop should look...

foreach($results as &$row) {    
   $row->date = ago($row->date);
}

Using the -> is how you access object properties.

Upvotes: 1

Related Questions