0

Basically I'm trying to sort a complex array of Objects within an array:

Array
(
    [190515] => stdClass Object
        (
            [nid] => 15740686
            [venue_nid] => 190515
            [occurrences] => 1
            [this_weeks_occurrences] => 0
            [end_date] => 1350853200
            [end_date_end_time] => 1350853200
            [is_ongoing] => 0
            [title] => Wentz Concert Hall and Fine Arts Center
            [times] => Array
                (
                    [0] => stdClass Object
                        (
                            [nid] => 15740686
                            [venue_nid] => 190515
                            [venue_title] => Wentz Concert Hall and Fine Arts Center
                            [datepart] => 20121021
                            [occurrences] => 1
                            [times] => Sun 4:00pm
                            [end_times] => Sun 4:00pm
                            [next_year] => 0
                            [next_month] => 0
                            [next_week] => 3
                            [occurrence_date] => 1350853200
                        )

                )

            [times_list] => Array
                (
                    [0] => Oct 21 sun 4:00pm
                )

        )

    [31403] => stdClass Object
        (
            [nid] => 15740686
            [venue_nid] => 31403
            [occurrences] => 1
            [this_weeks_occurrences] => 0
            [end_date] => 1350176400
            [end_date_end_time] => 1350176400
            [is_ongoing] => 0
            [title] => KAM Isaiah Israel
            [times] => Array
                (
                    [0] => stdClass Object
                        (
                            [nid] => 15740686
                            [venue_nid] => 31403
                            [venue_title] => KAM Isaiah Israel
                            [datepart] => 20121014
                            [occurrences] => 1
                            [times] => Sat 8:00pm
                            [end_times] => Sat 8:00pm
                            [next_year] => 0
                            [next_month] => 0
                            [next_week] => 2
                            [occurrence_date] => 1350176400
                        )

                )

            [times_list] => Array
                (
                    [0] => Oct 13 sat 8:00pm
                )

        )

    [33861] => stdClass Object
        (
            [nid] => 15740686
            [venue_nid] => 33861
            [occurrences] => 1
            [this_weeks_occurrences] => 0
            [end_date] => 1350781200
            [end_date_end_time] => 1350781200
            [is_ongoing] => 0
            [title] => Music Institute of Chicago, Nichols Concert Hall
            [times] => Array
                (
                    [0] => stdClass Object
                        (
                            [nid] => 15740686
                            [venue_nid] => 33861
                            [venue_title] => Music Institute of Chicago, Nichols Concert Hall
                            [datepart] => 20121021
                            [occurrences] => 1
                            [times] => Sat 8:00pm
                            [end_times] => Sat 8:00pm
                            [next_year] => 0
                            [next_month] => 0
                            [next_week] => 3
                            [occurrence_date] => 1350781200
                        )

                )

            [times_list] => Array
                (
                    [0] => Oct 20 sat 8:00pm
                )

        )

)

I need to sort by occurrence_date and ensure that the data in the top Object (e.g.190515) doesn't become corrupted with other objects and to include the possibility that there could be a 2nd occurrence_date for the "times" [array] of Objects.

I've seen similar sort arrays of objects by field here, but not with the depth of the array/object. I tried using usort but I don't think my syntax to the value is correct.

Basically what I tried.

function cmp($x, $y) {
    if ($x->occurrence_date > $y->occurrence_date) {
      return 1; }
    else {
      return -1; }
 }
usort($node->timeout_events_schedule->venues, 'cmp');

Thanks in advance

2
  • Why would you compare numbers with strcmp? Commented Oct 5, 2012 at 20:52
  • Originally I was sorting by times_list, but changed to occurrence_date. I'll update. Commented Oct 5, 2012 at 20:58

2 Answers 2

3

How about:

function compare($x,$y)
{
    if($x->times[0]->occurrence_date == $y->times[0]->occurrence_date)
        return 0;
    elseif($x->times[0]->occurrence_date < $y->times[0]->occurrence_date)
        return -1;
    else
        return 1;

}

uasort($your_array,'compare');

uasort() preserve your keys, unlike usort() http://www.php.net/manual/en/function.uasort.php

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

7 Comments

Well, after undeleting and completely changing your answer after 25 minutes one should expect it to be proper, but presented code will not work with sample data provided by OP. Thats why it is better to guide someone in writing own code that he will understand how it works, so it can be customized without further help.
my bad D: I'll do it that way next time
I tried this and it didn't sort. Also tried @dev-null-dweller's suggestion to use the var as: $x->times[0]->occurence_date and that killed php.
Sorry, I messed up the return of the compare function. I edited it.
Thanks, but even with the edit, it does not sort correctly. Could it be that we aren't "getting" to the variable? Does there need to be a [0] or something before ['times']?
|
2

Few tips to help you solve your problem:

  • you will need uasort function, to preserve associative keys
  • You can not access occurrence_date key directly from $x o r $y, you will need $x->times[0]->occurence_date
  • Before doing comparison, iterate through $x->times just to check if there are more entries, pick one that suits your needs.
  • since occurence_date is a number you don't have to use string comparison function

Good luck :)

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.