Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Natural sort an associative array?

Given an array of arrays, how would I natural sort the inner arrays based on their values?

Example array:

array (size=2)
  0 => 
    array (size=1)
      'manager' => string 'Manager 1' (length=9)
  1 => 
    array (size=1)
      'manager' => string 'Manager 3' (length=9)

Another example array:

array (size=2)
  0 => 
    array (size=1)
      'month' => string 'June' (length=4)
  1 => 
    array (size=1)
      'month' => string 'January' (length=7)

My first idea was to just natsort() them, but that expects a normal array. The next idea was to use array_multisort($array, SORT_NATURAL);, but that didn't work due to the associative arrays.

So, how could I sort the inner arrays in using natural sorting? Also, keeping array keys doesn't matter in this case.

EDIT:

Expected output of array 1 would be the same (since Manager 1 and Manager 3 are already in order):

array (size=2)
  0 => 
    array (size=1)
      'manager' => string 'Manager 1' (length=9)
  1 => 
    array (size=1)
      'manager' => string 'Manager 3' (length=9)

Expected output of array two would put January ahead of June (the 'natural' order):

// 0 and 1 keys can switch or stay the same, doesn't matter
array (size=2)
  0 => 
    array (size=1)
      'month' => string 'January' (length=4)
  1 => 
    array (size=1)
      'month' => string 'June' (length=7)
like image 937
Samsquanch Avatar asked Feb 04 '15 16:02

Samsquanch


People also ask

How do you sort an associative array?

The arsort() function sorts an associative array in descending order, according to the value. Tip: Use the asort() function to sort an associative array in ascending order, according to the value. Tip: Use the krsort() function to sort an associative array in descending order, according to the key.

What is natural order sorting in PHP?

natsort(array &$array ): bool. This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a "natural ordering".

Can you sort a multidimensional array?

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.

What is associative array with example?

Associative arrays are used to store key value pairs. For example, to store the marks of different subject of a student in an array, a numerically indexed array would not be the best choice.


1 Answers

Well!, You can simplify the function using natural sort functions like this:

usort($array, function($a, $b){
    return strnatcmp($a['manager'],$b['manager']); //Case sensitive
    //return strnatcasecmp($a['manager'],$b['manager']); //Case insensitive
});
like image 188
rub3ns Avatar answered Oct 23 '22 17:10

rub3ns