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:
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 orfalse
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.
Output:
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.
Output:
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.
Output:
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.
Output:
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.