Wednesday, June 29, 2016

Sorting a multidimensional associative array by key's value using usort in PHP

This post explains how a multidimensional associative array in PHP can be sorted by value of one of the associative keys. It can also be described as sorting an array of arrays by the keys of sub-arrays. Let us understand the situation with an example. Suppose there is a PHP array shown in the PHP code below:
<?php
$multi_array = array(array('name' => 'Orange', 'code' => '18plkp', 'value' => 15),
                     array('name' => 'Apple', 'code' => '45jklp', 'value' => 49),
                     array('name' => 'Guava', 'code' => '48pyhp', 'value' => 169)
                    );
var_dump($multi_array);
?>

The output of this code is as follows:
array (size=3)
  0 => 
    array (size=3)
      'name' => string 'Orange' (length=6)
      'code' => string '18plkp' (length=6)
      'value' => int 15
  1 => 
    array (size=3)
      'name' => string 'Apple' (length=5)
      'code' => string '45jklp' (length=6)
      'value' => int 49
  2 => 
    array (size=3)
      'name' => string 'Guava' (length=5)
      'code' => string '48pyhp' (length=6)
      'value' => int 169
This is an array which has 3 elements. These 3 elements are associative arrays. So, if you want to sort the array $multi_array with the value of associative key 'name', then how will you do it in PHP?
Read below for the solution.
So, after sorting the array by key 'name' in ascending order, the array will look like as follows:
array (size=3)
  0 => 
    array (size=3)
      'name' => string 'Apple' (length=5)
      'code' => string '45jklp' (length=6)
      'value' => int 49
  1 => 
    array (size=3)
      'name' => string 'Guava' (length=5)
      'code' => string '48pyhp' (length=6)
      'value' => int 169
  2 => 
    array (size=3)
      'name' => string 'Orange' (length=6)
      'code' => string '18plkp' (length=6)
      'value' => int 15
For achieving the above output after sorting, we will use a PHP function "usort" as follows:

usort($multi_array, function($a, $b){
    return strnatcmp($a['name'], $b['name']);
});


So, here is the full code and its explanation.

Code:
<?php
$multi_array = array(array('name' => 'Orange', 'code' => '18plkp', 'value' => 15),
                     array('name' => 'Apple', 'code' => '45jklp', 'value' => 49),
                     array('name' => 'Guava', 'code' => '48pyhp', 'value' => 169)
                    );
var_dump($multi_array);
usort($multi_array, function($a, $b){
    return strnatcmp($a['name'], $b['name']);
});
var_dump($multi_array);
?>


Explanation:
1. $multi_array is a multidimensional array which has 3 associative arrays as its elements.
2. The first var_dump($multi_array) dumps the array on the console.
3. Here is the main part where sorting is done. The usort function has the following documentation:
usort — Sort an array by values using a user-defined comparison function
    The first argument is the array variable that is to be sorted. The second argument is the user-supplied comparison function. In the comparison function, we have used "strnatcmp" to compare the array elements by key "name". If the sorting needs to be done on another key, then that key can be used instead of "name". The strnatcmp compares alphanumeric strings.
4. The second var_dump($multi_array) outputs the sorted array to the console.

Note: The second argument passed to usort in the above example is an anonymous function which was introduced in PHP 5.3. So, for PHP < 5.3, we have to define a callback function and then pass the function name as the second argument to usort as shown below:

<?php
function cmp($a, $b)
{ return strnatcmp($a['name'], $b['name']); }
$multi_array = array(array('name' => 'Orange', 'code' => '18plkp', 'value' => 15), array('name' => 'Apple', 'code' => '45jklp', 'value' => 49), array('name' => 'Guava', 'code' => '48pyhp', 'value' => 169) ); var_dump($multi_array); usort($multi_array, "cmp"); var_dump($multi_array); ?>


Hope, you will get some useful information from this post.
Thanks.

No comments:

Post a Comment