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.
The basic syntax of the array_multisort()
function is given with:
array_multisort(array1, sortorder, sorttype, array2, array3, ...)
Parameter | Description |
---|---|
array1 | Required. Specifies an array |
sortorder | Optional. Specifies the sorting order. Possible values:
|
sorttype | Optional. Specifies the type to use, when comparing elements. Possible values:
|
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
)