简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM