Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Sort a multidimensional array by number of items

I have an array such as:

Array (     [DEF] => Array         (             [0] => Array                 (                     [type] => 1                     [id] => 1212                     [name] => Jane Doe                     [current] => 1                 )              [1] => Array                 (                     [type] => 1                     [id] => 3123121                     [name] => Door                     [current] =>                  )         )      [ABC] => Array         (             [0] => Array                 (                     [type] => 1                     [id] => 1234                     [name] => John Doe                     [current] =>                  )         )      [WW] => Array         (             [0] => Array                 (                     [type] => 1                     [id] => 1212                     [name] => Jane Doe                     [current] => 1                 )              [1] => Array                 (                     [type] => 1                     [id] => 3123121                     [name] => Door                     [current] =>                  )              [2] => Array                 (                     [type] => 1                     [id] => 64646                     [name] => Floor                     [current] =>                  )         ) ) 

And I want to sort this array by number ( count() ) of inner-array items descending (i.e. most items first), so I will have this array:

Array (     [WW] => Array         (             [0] => Array                 (                     [type] => 1                     [id] => 1212                     [name] => Jane Doe                     [current] => 1                 )              [1] => Array                 (                     [type] => 1                     [id] => 3123121                     [name] => Door                     [current] =>                  )              [2] => Array                 (                     [type] => 1                     [id] => 64646                     [name] => Floor                     [current] =>                  )         )      [DEF] => Array         (             [0] => Array                 (                     [type] => 1                     [id] => 1212                     [name] => Jane Doe                     [current] => 1                 )              [1] => Array                 (                     [type] => 1                     [id] => 3123121                     [name] => Door                     [current] =>                  )         )      [ABC] => Array         (             [0] => Array                 (                     [type] => 1                     [id] => 1234                     [name] => John Doe                     [current] =>                  )         ) ) 

Can anyone suggest an efficient way to do so? Thanks.

like image 658
Aviram Avatar asked Sep 15 '11 15:09

Aviram


People also ask

How do I sort a multi dimensional 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.

Can we sort multidimensional array?

A two-dimensional or 2D array is a collection of columns and rows. Programmers can randomly access the 2D array elements or each cell individually by utilizing their indexes. With the help of sorting, array elements are arranged according to the requirements, whether in ascending or descending order.

Which PHP function is used to sort multi dimensional arrays?

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 you sort an array of objects in PHP?

The usort() function is an inbuilt function in PHP which is used to sort the array of elements conditionally with a given comparator function. The usort() function can also be used to sort an array of objects by object field.


1 Answers

Using uksort:

uksort($array, function($a, $b) { return count($b) - count($a); }); 

Using array_multisort:

array_multisort(array_map('count', $array), SORT_DESC, $array); 

With PHP < 5.3:

function sort_cb($a, $b) {     return count($b) - count($a); } uksort($array, 'sort_cb'); 
like image 84
Arnaud Le Blanc Avatar answered Sep 19 '22 13:09

Arnaud Le Blanc