Home >Backend Development >PHP Tutorial >array_multisort() function in PHP
array_multisort() function is used to sort multiple or multi-dimensional arrays. It returns a sorted array.
array_multisort(arr1, sort_order, sort_type, arr2, arr3, arr4...)
arr1 − Array to be sorted
sort_order − Sort order. The following are possible values
- SORT_ASC - Default. Sort in ascending order (A-Z)
- SORT_DESC - Sort in descending order (Z-A)
sort_type − Sorting behavior. The following are possible values
SORT_REGULAR - Default. Compare elements in the normal way (standard ASCII)
SORT_NUMERIC - Compare elements as numeric values
SORT_STRING - Compare elements as strings
SORT_LOCALE_STRING - Compares elements as strings, based on the current locale (can be changed using setlocale())
SORT_NATURAL - Use "Natural sorting" compares elements as strings, similar to natsort()
SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL for case-insensitivity String sorting.
arr2 − Another array. Optional
arr3 − Another array. Optional.
arr4 − Another array. Optional.
array_multisort() function returns a sorted array.
Demonstration
<?php $a1 = array(12, 55, 3, 9, 99); $a2 = array(44, 67, 22, 78, 46); array_multisort($a1,$a2); print_r($a1); print_r($a2); ?>
Array ( [0] => 3 [1] => 9 [2] => 12 [3] => 55 [4] => 99 ) Array ( [0] => 22 [1] => 78 [2] => 44 [3] => 67 [4] => 46 )
Let us see another example of merging two arrays and sorting them in ascending order An example.
Online demonstration
<?php $a1 = array(12, 55, 3, 9, 99); $a2 = array(44, 67, 22, 78, 46); $num = array_merge($a1,$a2); array_multisort($num,SORT_ASC,SORT_NUMERIC); print_r($num); ?>
Array ( [0] => 3 [1] => 9 [2] => 12 [3] => 22 [4] => 44 [5] => 46 [6] => 55 [7] => 67 [8] => 78 [9] => 99 )
The above is the detailed content of array_multisort() function in PHP. For more information, please follow other related articles on the PHP Chinese website!