PHP | array_multisort() Function - GeeksforGeeks

18 Mar, 2018
The array_multisort() is an inbuilt function in PHP which is used to sort multiple arrays at once or a multi-dimensional array with each individual dimension.With this function, one should remember that string keys will be maintained, but numeric keys will be re-indexed, starting at 0 and increases by 1.
Syntax:
bool array_multisort($array1, sorting_order, sorting_type, $array2..)
Parameters: The array generally takes one parameter that is the array which needs to be sorted. But in addition, the function can take two more optional parameters sorting_order and sorting_type.
$array1: This parameter specifies the array which we want to sort.sorting_order: This parameter specifies the order to use i.e. in ascending or descending order. The default value of this parameter is SORT_ASC. That is, sorting in ascending order. In order to sort in descending order we will have to set this parameter to SORT_DESC.sorting_type: This parameter specifies the sort options for the arrays and they are as follows:SORT_REGULAR: Compare elements regularly(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.SORT_NATURAL: Compare elements as strings using “natural ordering”.SORT_FLAG_CASE: Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.If we want to sort multiple arrays, we can pass them as parameters like $array2, $array3... followed by their sorting_order, sorting_type.
$array1: This parameter specifies the array which we want to sort.
sorting_order: This parameter specifies the order to use i.e. in ascending or descending order. The default value of this parameter is SORT_ASC. That is, sorting in ascending order. In order to sort in descending order we will have to set this parameter to SORT_DESC.
sorting_type: This parameter specifies the sort options for the arrays and they are as follows:SORT_REGULAR: Compare elements regularly(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.SORT_NATURAL: Compare elements as strings using “natural ordering”.SORT_FLAG_CASE: Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.If we want to sort multiple arrays, we can pass them as parameters like $array2, $array3... followed by their sorting_order, sorting_type.
SORT_REGULAR: Compare elements regularly(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.
SORT_NATURAL: Compare elements as strings using “natural ordering”.
SORT_FLAG_CASE: Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.
If we want to sort multiple arrays, we can pass them as parameters like $array2, $array3... followed by their sorting_order, sorting_type.
Return value: The array_multisort() function returns a boolean value. That is it will return TRUE on success and FALSE on failure.
Note:If two members on comparing become equal, their relative order in sorted array is undefined.
Below programs illustrate the array_multisort() function:
Program 1:
<?php  // Input array$animals = array("Dog", "Cat", "Horse",                 "Bear", "Zebra", "Lion");  // sorting array using default values// for sorting_order and sorting_type    array_multisort($animals);          print_r($animals);          ?>
Output:
Array
(
    [0] => Bear
    [1] => Cat
    [2] => Dog
    [3] => Horse
    [4] => Lion
    [5] => Zebra
)

Program 2:
<?php  // Input arrays$array1=array("Dog", "Cat");$array2=array("Fido", "Missy");  // sorting multiple arrays using default values// for sorting_order and sorting_type         array_multisort($array1, $array2);  // printing sorted arrays         print_r($array1);print_r($array2);  ?> 
Output:
Array
(
    [0] => Cat
    [1] => Dog
)
Array
(
    [0] => Missy
    [1] => Fido
)

Program 3:
<?php  // Input arrays$array1=array("Dog", "Dog", "Cat");$array2=array("Pluto", "Fido", "Missy");  // sorting multiple arrays       array_multisort($array1, SORT_ASC, $array2, SORT_DESC);  // Printing sorted arrays       print_r($array1);print_r($array2);  ?> 
Output:
Array
(
    [0] => Cat
    [1] => Dog
    [2] => Dog
)
Array
(
    [0] => Missy
    [1] => Pluto
    [2] => Fido
)

Reference:http://php.net/manual/en/function.array-multisort.php
PHP-array
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
How to receive JSON POST with PHP ?
How to pass JavaScript variables to PHP ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills