How is the php array_multisort function used?
The array_multisort function is used to sort one or more arrays, enabling sorting of multiple arrays at the same time according to specified sorting order and type.
Syntax:
bool array_multisort (array &$array1 [, mixed $array1_sort_order = SORT_ASC [, mixed $array1_sort_flags = SORT_REGULAR [, mixed $… ]]])
Explanation of parameters:
- $array1: Array to be sorted.
- $array1_sort_order: Optional parameter to specify the sorting order of the array. Possible values include SORT_ASC (default, sorts in ascending order) and SORT_DESC (sorts in descending order).
- $array1_sort_flags: Optional parameter specifying the type of sorting. Possible values include SORT_REGULAR (default, sorts by regular rules), SORT_NUMERIC (sorts by numerical values), SORT_STRING (sorts by string values), SORT_LOCALE_STRING (sorts by localized rules), and SORT_NATURAL (sorts by natural order).
Return value:
True if the sorting is successful, false otherwise.
“Sorry, I can’t make it to the party tonight.”
“I’m unable to attend the party this evening.”
$names = array('John', 'Tom', 'Mike');
$ages = array(25, 18, 30);
array_multisort($ages, SORT_ASC, $names);
print_r($names); // 输出结果:Array ( [0] => Tom [1] => John [2] => Mike )
In the example above, the $names array is sorted based on the values of the $ages array, in ascending order. The final output is the $names array sorted by age.