-1

I have searched this site, looking for answers, but I cannot make it work. So finally I post this question knowing that there are a lot of possible duplicates. But when I try to use the answers, I get error messages of stdClass

I have an array with these values:

Array
(
    [1251] => stdClass Object
        (
            [vid] => 1253
            [uid] => 20
            [body] => Array
                (
                )

            [field_datum] => Array
                (
                    [und] => Array
                        (
                            [0] => Array
                                (
                                    [value] => 2016-09-17T11:30:00
                                    [timezone] => Europe/Brussels
                                    [timezone_db] => UTC
                                    [date_type] => date
                                )

                        )

                )
        )

I would have to sort this array with the value of the field_datum [field_datum][und][0][value]

I have tried this sollution: Sorting by key in a multidimensional array with php

But I get this as an error Fatal error: Cannot use object of type stdClass as array

3
  • post code that causes an error. Commented Aug 26, 2016 at 13:08
  • 1
    Error because you use a object and the solution use array. class->attribute, no class['attribute']. And the examble is very different that you case. Commented Aug 26, 2016 at 13:11
  • You need iterate to array and get value date and convert this value to timestamp and create new array[timestamp1][0]=>object, array[timestamp1][1]=>object, array[timestamp2][0]=>object.. and them sort array. Commented Aug 26, 2016 at 13:14

1 Answer 1

0

With all the answers you gave, I found a solution

function cmp($a, $b) {
  if ($a->field_datum == $b->field_datum) {
    return 0;
  } else {
    return $b->field_datum < $a->field_datum ? 1 : -1; 
  }
}

usort($infodagen, 'cmp');
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.