PHP array_multisort() Function

0

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:

  • SORT_ASC – Default. Sort in ascending order (A-Z)
  • SORT_DESC – Sort in descending order (Z-A)
sorting type Optional. Specifies the type to use, when comparing elements. Possible values:

  • SORT_REGULAR – Default. Compare elements normally (Standard ASCII)
  • SORT_NUMERIC – Compare elements as numeric values
  • SORT_STRING – Compare elements as string values
  • SORT_LOCALE_STRING – Compare elements as string, based on the current locale (can be changed using setlocale())
  • SORT_NATURAL – Compare elements as strings using “natural ordering” like natsort()
  • SORT_FLAG_CASE – Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively
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 )