0

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?

3
  • You need to fetch data with mysql_fetch_* functions. Your $result variable is a resource (php.net/manual/en/function.mysql-query.php) Commented Jun 7, 2016 at 20:19
  • you should NOT be getting that result at all. you're json_encoding a mysql result handle, which is NOT the results themselves. Commented Jun 7, 2016 at 20:21
  • I removed the top section of the question. It was a simplified version. Let just say that I am getting the results and I need to replace the date. Commented Jun 7, 2016 at 20:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

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.