PHP array_multisort() Function

PHP array_multisort Function


Definition :

The array_multisort() function sort multiple arrays at once, or a multi-dimensional array by one or more dimensions. The sorting is done as though the arrays were columns in a table.

Note: String keys will be maintained, but numeric keys will be re-indexed, starting at 0 and increase by 1.

Note: You can assign the sortorder and the sorttype parameters after each array. If not specified, each array parameter uses the default values.

Syntax

The basic syntax of the array_multisort() function is given with:

          array_multisort(array1, sortorder, sorttype, array2, array3, ...)

Parameter Values
Parameter Description
array1 Required. Specifies an array
sortorder Optional. Specifies the sorting order. Possible values:
  • SORT_ASC - Default. Sort in ascending order (A-Z)
  • SORT_DESC - Sort in descending order (Z-A)
sorttype 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

 

Example1 : In this example we can see Return a sorted array in ascending order:

Output :

Array
(
    [0] => Designing
    [1] => Web
)
Array
(
    [0] => Promotion
    [1] => House
)

 

Example2 : Now understand when  when two values are the same

Output :

Array
(
    [0] => Designing
    [1] => Web
    [2] => Web
)
Array
(
    [0] => Online
    [1] => House
    [2] => Promotion
)

Example3 : Now we can see Merge two arrays and sort them as numbers, in descending order(using parameters)

Output :

Array
(
    [0] => 66
    [1] => 41
    [2] => 30
    [3] => 30
    [4] => 25
    [5] => 20
    [6] => 15
    [7] => 7
    [8] => 4
    [9] => 1
)