The array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.
Note: Associative (string) keys will be maintained, but numeric keys will be re-indexed.
Parameter | Description |
---|---|
array1 | Required. Specifies an array |
sorting order | Optional. Specifies the sorting order. Possible values:
|
sorting type | Optional. Specifies the type to use, when comparing elements. Possible values:
|
array2 | Optional. Specifies an array |
array3 | Optional. Specifies an array |
Syntax
array_multisort(array1,sorting order,sorting type,array2,array3...)
Example
$array1 = array("Dog","Cat");
$array2 = array("Samuel","Cooper");
array_multisort($array1,$array2);
print_r($array1);
print_r($array2);
Output
Array ( [0] => Cat [1] => Dog ) Array ( [0] => Cooper [1] => Samuel )
More Example
$array1 = array("Dog","Cat");
$array2 = array("Cooper","Samuel");
array_multisort($array1,SORT_ASC,$array2,SORT_DESC);
print_r($array1);
print_r($array2);
Output
Array ( [0] => Cat [1] => Dog ) Array ( [0] => Samuel [1] => Cooper )