Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort multidimensional array by multiple columns

I'm trying to sort a multidimensional array by multiple keys, and I have no idea where to start. I looked at uasort(), but wasn't quite sure how to write a function for what I need.

I need to sort by the state, then event_type, then date_start.

My array looks like this:

[     ['ID' => 1, 'title' => 'Boring Meeting',  'date_start' => '2010-07-30', 'event_type' => 'meeting', 'state' => 'new-york'],     ['ID' => 2, 'title' => 'Find My Stapler', 'date_start' => '2010-07-22', 'event_type' => 'meeting', 'state' => 'new-york'],     ['ID' => 3, 'title' => 'Mario Party',     'date_start' => '2010-07-22', 'event_type' => 'party',   'state' => 'new-york'],     ['ID' => 4, 'title' => 'Duct Tape Party', 'date_start' => '2010-07-28', 'event_type' => 'party',   'state' => 'california'] ] 

My desired result is:

[     ['ID' => 4, 'title' => 'Duct Tape Party', 'date_start' => '2010-07-28', 'event_type' => 'party',   'state' => 'california']     ['ID' => 2, 'title' => 'Find My Stapler', 'date_start' => '2010-07-22', 'event_type' => 'meeting', 'state' => 'new-york'],     ['ID' => 1, 'title' => 'Boring Meeting',  'date_start' => '2010-07-30', 'event_type' => 'meeting', 'state' => 'new-york'],     ['ID' => 3, 'title' => 'Mario Party',     'date_start' => '2010-07-22', 'event_type' => 'party',   'state' => 'new-york'], ] 
like image 878
attepted_nerd Avatar asked Jul 12 '10 23:07

attepted_nerd


People also ask

Can you sort a multidimensional array?

array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions. Associative (string) keys will be maintained, but numeric keys will be re-indexed.

How do I sort a multidimensional array in PHP?

Sorting a multidimensional array by element containing date. Use the usort() function to sort the array. The usort() function is PHP builtin function that sorts a given array using user-defined comparison function. This function assigns new integral keys starting from zero to array elements.

Which function sort multiple array at once?

The array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.

How do you sort a 2D array?

Make the 2D array into a separate simple (1D) array (STEP 1). Then use the Arrays. sort() method to sort the simple array (STEP 2). Then set each space of the 2D array to be the number of columns across (X-coordinate where the space will be changed) multiplied by the number of spaces per row in the 2D array.


1 Answers

You need array_multisort

$mylist = array(     array('ID' => 1, 'title' => 'Boring Meeting', 'event_type' => 'meeting'),     array('ID' => 2, 'title' => 'Find My Stapler', 'event_type' => 'meeting'),     array('ID' => 3, 'title' => 'Mario Party', 'event_type' => 'party'),     array('ID' => 4, 'title' => 'Duct Tape Party', 'event_type' => 'party') );  # get a list of sort columns and their data to pass to array_multisort $sort = array(); foreach($mylist as $k=>$v) {     $sort['title'][$k] = $v['title'];     $sort['event_type'][$k] = $v['event_type']; } # sort by event_type desc and then title asc array_multisort($sort['event_type'], SORT_DESC, $sort['title'], SORT_ASC,$mylist); 

As of PHP 5.5.0:

array_multisort(array_column($mylist, 'event_type'), SORT_DESC,                 array_column($mylist, 'title'),      SORT_ASC,                 $mylist); 

$mylist is now:

array (   0 =>    array (     'ID' => 4,     'title' => 'Duct Tape Party',     'event_type' => 'party',   ),   1 =>    array (     'ID' => 3,     'title' => 'Mario Party',     'event_type' => 'party',   ),   2 =>    array (     'ID' => 1,     'title' => 'Boring Meeting',     'event_type' => 'meeting',   ),   3 =>    array (     'ID' => 2,     'title' => 'Find My Stapler',     'event_type' => 'meeting',   ), ) 
like image 159
Rob Avatar answered Oct 14 '22 15:10

Rob