Categories
PHP

Sorting Multidimensional and Multiple Arrays with array_multisort

In this tutorial, you’ll learn how to use the array_multisort() function to sort multiple arrays depending on the sort order of another array. You’ll also learn to use this function to sort multidimensional arrays in one or more dimensions.

This tutorial covers the following topics:

  1. The array_multisort function
  2. Sorting multiple arrays based on each other.
  3. Sorting multidimensional arrays.
<?php
//Syntax
array_multisort(
    array &$array1,
    mixed $array1_sort_order = SORT_ASC,
    mixed $array1_sort_flags = SORT_REGULAR,
    mixed ...$rest
): bool
  1. $array1
    The first argument to array_multisort() is the array that you want to sort first.
  2. $array_sort_order (optional)
    Use the constant SORT_ASC or SORT_DESC for ascending or descending, respectively. The default sorting order is SORT_ASC.
  3. $array1_sort_flags (optional)
    You can also use the sorting flags to change the sorting behavior. Click to read about the list of sort_flags constants.
  4. …$rest
    Other arrays that you want to be sorted in sync with the first. Each subsequent array can also be followed by the optional arguments (sort_order and sort_flags).

Example: array_multisort

<?php
array_multisort($arr1, SORT_DESC, SORT_STRING,
                $arr2, SORT_NUMERIC,
                $arr3, SORT_STRING,
                $arr4, SORT_ASC, SORT_NATURAL,
                $arr5);

Sorting Mulitple Arrays

The array_multisort() function accepts multiple arrays and uses them as sort criteria. Sorting begins with the first array; values in that array that evaluate as equal are sorted by the next array, and so on.

<?php
 $array1 = ['one', 'two', 'three'];
 $array2 = ['I', 'II', 'III'];
 $array3 = [1, 2 , 3];

 array_multisort($array1,
                 $array2, 
                 $array3);

 print_r($array1);
 print_r($array2);
 print_r($array3);

In the above example, arrays keep in sync when reordered. The following output prints on the browser:

Array
(
    [0] => one
    [1] => three
    [2] => two
)
Array
(
    [0] => I
    [1] => III
    [2] => II
)
Array
(
    [0] => 1
    [1] => 3
    [2] => 2
)

Sorting Multidimensional Arrays

This function comes in handy when dealing with symmetrical multidimensional arrays, like the one in the following example:

<?php
//Note: p key represents the population
$data = [
['city' => 'Tokyo',    'p' => '37,435,191'],
['city' => 'Delhi',    'p' => '29,399,141'],
['city' => 'Shanghai', 'p' => '26,317,104'],
['city' => 'Sao Paulo','p' => '21,846,507'],
['city' => 'Mexico City', 'p' => '21,671,908'],
['city' => 'Cairo',   'p' => '20,484,965'],
['city' => 'Dhaka',   'p' => '20,283,552'],
['city' => 'Mumbai',  'p' => '20,185,064'],
['city' => 'Beijing', 'p' => '20,035,455'],
['city' => 'Osaka',   'p' => '19,222,665']
];

We want the $data array to be sorted by cities in ascending order, to accomplish this, we need an array that contains the same cities name in the same order as in the $data array. This can be done easily by using the array_column function. The array_column takes two arguments: the array and the key you want to extract from the multidimensional array.

<?php
 //$data = [['city'=>'Tokyo, 'p' => '2...];
 $cities = array_column ($data, 'city');

Now we can use array_multisort() to sort the multidimensional array $data. The first argument needs to be the $cities array, followed by the sort direction, and then the $data array:

<?php
//Note: p key represents the population
$data = [
['city' => 'Tokyo',    'p' => '37,435,191'],
['city' => 'Delhi',    'p' => '29,399,141'],
['city' => 'Shanghai', 'p' => '26,317,104'],
['city' => 'Sao Paulo','p' => '21,846,507'],
['city' => 'Mexico City', 'p' => '21,671,908'],
['city' => 'Cairo',   'p' => '20,484,965'],
['city' => 'Dhaka',   'p' => '20,283,552'],
['city' => 'Mumbai',  'p' => '20,185,064'],
['city' => 'Beijing', 'p' => '20,035,455'],
['city' => 'Osaka',   'p' => '19,222,665']
];

$cities = array_column ($data, 'city');

array_multisort($cities, SORT_ASC,
                $data);
foreach ($data as $v){
 echo $v['city'] .' : '. $v['p'];
 echo '<br>';
}

Edit the above code if you want to sort the $data array by population, and replace relevant lines with the following code:

<?php

$population = array_column ($data, 'p');
array_multisort($population, SORT_ASC,
                $data);

More Posts on PHP Sorting Arrays: