PHP array_multisort() Function

6 Jan 2025 | 2 min read

The array_multisort() is an built-in function in PHP. This function is used to sort multiple arrays at once or a multi-dimensional array with each dimension. This function was introduced in PHP 4.0.

Syntax

Parameters

ParameterDescriptionRequired/Optional
array1Specifies an array.compulsory
Sorting orderSpecifies the sorting order. Possible values:
  • SORT_ASC - Default. Sort in ascending order (A-Z)
  • SORT_DESC -Sort in descending order (Z-A)
Optional
Sorting typeSpecifies 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 a 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
Optional
array2Specifies an array.Optional
array3Specifies an array.Optional

Return Type

The array_multisort()function returns true on success or false on failure.

Important note: String keys will be maintained, but numeric keys will be re-indexed, starting at 0 and increase by 1. If two members on comparing become equal, their relative order in the sorted array is undefined.

Example 1

Output:

 Array ( [0] =>dhoni [1] =>ganguly [2] =>sachin [3] =>umesh [4] =>virat [5] =>yuvraj ) 

Example 2

Output:

 Array ( [0] => Dell [1] => Lenovo ) Array ( [0] => Asus [1] => Acer ) 

Example 3

Output:

 Array ( [0] => java [1] =>php [2] =>php ) Array ( [0] =>perl [1] => python [2] =>swift) 

Example 4

Output:

 Array ( [0] => java [1] =>php [2] =>php ) Array ( [0] => python [1] => swift [2] =>perl )