PHP array_multisort() function


The array_multisort() function in PHP is used to sort multiple arrays or multi-dimensional arrays at once. It sorts arrays in parallel, meaning it sorts multiple arrays based on one or more criteria (for example, sorting an array of names in alphabetical order while maintaining the corresponding age array). It can also be used to sort multidimensional arrays by one or more of their columns.

Syntax:

array_multisort(array &$array1, int|array $sort_order = SORT_ASC, int|array $sort_flags = SORT_REGULAR, array ...$arrays): bool

Parameters:

  • $array1: The first array to be sorted (by reference). This is the primary array that will be sorted.
  • $sort_order (optional): Sorting order. It can be:
    • SORT_ASC (default): for ascending order.
    • SORT_DESC: for descending order.
  • $sort_flags (optional): A flag that defines how to compare the elements. Common values are:
    • SORT_REGULAR: Compare items normally (default).
    • SORT_NUMERIC: Compare items numerically.
    • SORT_STRING: Compare items as strings.
    • SORT_NATURAL: Compare items using "natural order" algorithm (like how people expect things to be sorted, e.g., item1, item2, item10).
    • SORT_LOCALE_STRING: Compare items as strings based on the current locale.
  • ...$arrays: Additional arrays to be sorted along with $array1. They are sorted according to the same criteria, maintaining parallel relationships.

Return Value:

  • Returns true on success or false on failure.

Key Concepts:

  • Parallel Sorting: If multiple arrays are passed, the first array is sorted as per the provided order and flags, and all subsequent arrays are re-ordered to maintain their correlation with the first array.
  • Multidimensional Sorting: When sorting a multidimensional array, you can use array_multisort() to sort by a specific column.

Example 1: Sorting Two Related Arrays

In this example, the names array is sorted in ascending order, and the ages array is sorted accordingly to maintain the correlation.

<?php $names = ["John", "Alice", "Bob"]; $ages = [35, 25, 30]; array_multisort($names, SORT_ASC, $ages); print_r($names); print_r($ages); ?>

Output:

Array ( [0] => Alice [1] => Bob [2] => John ) Array ( [0] => 25 [1] => 30 [2] => 35 )

Here, the names array is sorted alphabetically, and the ages array is re-arranged to maintain the correct age corresponding to each name.

Example 2: Sorting by Numeric Values

You can sort numeric arrays as well. In this case, we use SORT_NUMERIC to explicitly specify numeric sorting.

<?php $numbers = [3, 1, 4, 1, 5, 9]; array_multisort($numbers, SORT_NUMERIC); print_r($numbers); ?>

Output:

Array ( [0] => 1 [1] => 1 [2] => 3 [3] => 4 [4] => 5 [5] => 9 )

The numbers array is sorted in ascending numeric order.

Example 3: Sorting Multidimensional Arrays

You can sort a multidimensional array by one of its columns. Suppose you have an array of people with their names, ages, and heights.

<?php $people = [ ["name" => "John", "age" => 35, "height" => 180], ["name" => "Alice", "age" => 25, "height" => 170], ["name" => "Bob", "age" => 30, "height" => 175] ]; // Extract the columns to sort by $names = array_column($people, 'name'); $ages = array_column($people, 'age'); $heights = array_column($people, 'height'); // Sort by age (ascending) and then by height (descending) array_multisort($ages, SORT_ASC, $heights, SORT_DESC, $people); print_r($people); ?>

Output:

Array ( [0] => Array ( [name] => Alice [age] => 25 [height] => 170 ) [1] => Array ( [name] => Bob [age] => 30 [height] => 175 ) [2] => Array ( [name] => John [age] => 35 [height] => 180 ) )

In this example, the array of people is sorted by age first, and in cases where ages are the same, it will sort by height in descending order.

Example 4: Sorting with Different Sort Orders and Flags

You can use different sorting orders and flags for each array.

<?php $fruits = ["apple", "orange", "banana"]; $quantities = [10, 2, 5]; // Sort fruits in descending order, quantities in ascending numeric order array_multisort($fruits, SORT_DESC, $quantities, SORT_NUMERIC); print_r($fruits); print_r($quantities); ?>

Output:

Array ( [0] => orange [1] => banana [2] => apple ) Array ( [0] => 2 [1] => 5 [2] => 10 )

Here, the fruits array is sorted alphabetically in descending order, and the quantities array is sorted in ascending numeric order.

Practical Usage:

  • array_multisort() is useful for:
    • Sorting multiple related arrays simultaneously (e.g., sorting product names and prices together).
    • Sorting multidimensional arrays by specific columns.
    • Handling large datasets that require sorting by multiple criteria.

Summary:

  • array_multisort() sorts multiple arrays in parallel or sorts multidimensional arrays by specific columns.
  • It can accept multiple sorting orders (SORT_ASC, SORT_DESC) and comparison flags (SORT_REGULAR, SORT_NUMERIC, SORT_STRING, etc.).
  • If sorting multidimensional arrays, columns can be extracted and passed to array_multisort() for sorting.
  • Numeric keys are re-indexed, and arrays are sorted according to the order of the first array, with subsequent arrays maintaining the same relationship as before sorting.