English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The PHP array_multisort() function sorts multiple arrays or multidimensional arrays
array_multisort(array1,sorting order,sorting type,array2...);
array_multisort() can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one or more dimensions.
The associated (string) key name remains unchanged, but the numeric key name will be reindexed.
Serial number | Parameters and descriptions |
---|---|
1 | array1(required) It specifies an array |
2 | sorting order (optional) It specifies the sorting order. Possible values-
|
3 | sorting type (optional) When comparing elements, it specifies the type to be used. Possible values:
|
4 | array2(Optional) It specifies an array |
Success, returns TRUE; failure, returns FALSE.
1Multiple Arrays Sorting Example
<?php $input1 = array("10" 100, 100, "a"); $input2 = array(1, 3",2" 1); array_multisort($input1, $input2); print_r($input1); print_r($input2); ?>Test and See‹/›
Output Result:
Array ( [0] => 10 [1=> a [2=> 100 [3=> 100 ) Array ( [0] => 1 [1=> 1 [2=> 2 [3=> 3 )
2Sorting Multi-dimensional Arrays Example
<?php $ar = array( array("10" 11, 100, 100, "a"), array( 1, 2",2" 3, 1) ); array_multisort($ar[0], SORT_ASC, SORT_STRING, $ar[1], SORT_NUMERIC, SORT_DESC); var_dump($ar); ?>Test and See ‹/›
Output Result:
array(2) { [0]=> array(5) { [0]=> string(2) "10" [1=> int(100) [2=> int(100) [3=> int(11) [4=> string(1) "a" } [1=> array(5) { [0]=> int(1) [1=> int(3) [2=> string(1) "2" [3=> int(2) [4=> int(1) } }
3Case-insensitive Sorting of Arrays
<?php $array = array('Alpha', 'atomic', 'Beta', 'bank'); $array_lowercase = array_map('strtolower', $array); array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array); print_r($array); ?>Test and See ‹/›
Output Result:
Array ( [0] => Alpha [1=> atomic [2=> bank [3=> Beta )