PHP Array Sort By Object?

I've got an array that looks like this...

[field_room_rates] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [target_id] => 4110
                            [entity] => stdClass Object
                                (
                                    [vid] => 4110
                                    [body] => Array
                                        (
                                        )

                                    [field_rate_dates] => Array
                                        (
                                            [und] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [value] => 2015-11-02 00:00:00
                                                            [value2] => 2015-11-26 00:00:00
                                                            [timezone] => America/New_York
                                                            [timezone_db] => America/New_York
                                                            [date_type] => datetime
                                                        )

                                                )

                                        )

Open in new window


So - the primary array is field_room_rates - which has an object within it called Entity.

I'd like to sort the values of field_room_rates['und'] by the date contained within ['entity']->field_rate_dates['und'][0]['value']

..and I'm having a bit of difficulty with the array sort syntax.

How would you go about doing that?  :)
LVL 2
erzoolanderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
I have shortened the path to the target array - but the principle remains the same
The solution uses the usort function which allows for a custom comparrison of the data. We then use a simple difference of the UNIX time stamps of each of the date as the return value
<?php
$field_rate_dates = array (
  'und' => array (
    array(
      'value' => '2015-11-02 00:00:00',
      'value2' => '2015-11-26 00:00:00',
      'timezone' => 'America/New_York',
      'timezone_db' => 'America/New_York',
      'date_type' => 'datetime'
    ),
    array(
      'value' => '2015-10-02 00:00:00',
      'value2' => '2015-11-26 00:00:00',
      'timezone' => 'America/New_York',
      'timezone_db' => 'America/New_York',
      'date_type' => 'datetime'
    ),
    array(
      'value' => '2015-02-02 00:00:00',
      'value2' => '2015-11-26 00:00:00',
      'timezone' => 'America/New_York',
      'timezone_db' => 'America/New_York',
      'date_type' => 'datetime'
    )
  )
);

usort($field_rate_dates['und'], function($a, $b) {
  return strtotime($a['value']) - strtotime($b['value']);
});
echo "<pre>";
print_r($field_rate_dates);
echo "</pre>";

Open in new window


Results
Array
(
    [und] => Array
        (
            [0] => Array
                (
                    [value] => 2015-02-02 00:00:00
                    [value2] => 2015-11-26 00:00:00
                    [timezone] => America/New_York
                    [timezone_db] => America/New_York
                    [date_type] => datetime
                )

            [1] => Array
                (
                    [value] => 2015-10-02 00:00:00
                    [value2] => 2015-11-26 00:00:00
                    [timezone] => America/New_York
                    [timezone_db] => America/New_York
                    [date_type] => datetime
                )

            [2] => Array
                (
                    [value] => 2015-11-02 00:00:00
                    [value2] => 2015-11-26 00:00:00
                    [timezone] => America/New_York
                    [timezone_db] => America/New_York
                    [date_type] => datetime
                )

        )

)

Open in new window

0
erzoolanderAuthor Commented:
It isn't the field_room_rates key that I'm wanting to sort, though.  I'm wanting to sort the field_room_rates key['und'] key (which proceeds it in the hierarchy).  Maybe I didn't paste enough of the array for my question to make sense.  :)

[field_room_rates] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [entity] => stdClass Object
                                (
                                    [field_rate_dates] => Array
                                        (
                                            [und] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [value] => 2015-11-02 00:00:00
                                                            [value2] => 2015-11-26 00:00:00
                                                        )
                                                )
                                        )
                                )
                        )
                    [1] => Array
                        (
                            [entity] => stdClass Object
                                (
                                    [field_rate_dates] => Array
                                        (
                                            [und] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [value] => 2015-11-27 00:00:00
                                                            [value2] => 2015-11-29 00:00:00
                                                        )
                                                )
                                        )
                                )
                        )

                    [2] => Array
                        (
                            [entity] => stdClass Object
                                (
                                    [field_rate_dates] => Array
                                        (
                                            [und] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [value] => 2015-11-30 00:00:00
                                                            [value2] => 2015-12-15 00:00:00
                                                        )
                                                )
                                        )
                                )
                        )

Open in new window


So - field_room_rates['und'] is the array I'm trying to sort (items 0, 1, 2, 3, etc).

Within that array is an object called entities...which has within it an array called field_rate_dates['und'].  I want to sort the top-level array by the contained object array's value.  :)
0
Julian HansenCommented:
Minor change (the data was already sorted so I changed two around)
<?php
$field_room_rates = array (
	'und' => array ()
);

$obj = new stdClass;
$obj->field_rate_dates = array (
	'und' => array (
		array (
			'value' => '2015-11-02 00:00:00',
			'value2' => '2015-11-26 00:00:00',
		)
	)
);
$field_room_rates['und'][] = array ('entity' => $obj);
$obj = new stdClass;
$obj->field_rate_dates = array (
	'und' => array (
		array (
			'value' => '2015-11-30 00:00:00',
			'value2' => '2015-11-29 00:00:00',
		)
	)
);
$field_room_rates['und'][] = array ('entity' => $obj);
$obj = new stdClass;
$obj->field_rate_dates = array (
	'und' => array (
		array (
			'value' => '2015-11-27 00:00:00',
			'value2' => '2015-12-15 00:00:00',
		)
	)
);
$field_room_rates['und'][] = array ('entity' => $obj);

$tosort = $field_room_rates['und'];
usort($tosort, function($a, $b) {
  return strtotime($a['entity']->field_rate_dates['und'][0]['value']) - strtotime($b['entity']->field_rate_dates['und'][0]['value']);
});

echo "<pre>" . print_r($tosort, true) . "</pre>";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

erzoolanderAuthor Commented:
Perfect- gracias!
0
Julian HansenCommented:
You are welcome.
0
Ray PaseurCommented:
E-E has an article that covers this question - might be helpful for future reference.
http://www.experts-exchange.com/articles/18319/Using-PHP-usort-to-Simplify-Complex-Data-Search.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.