PHP array_multisort() Function
Complete PHP Array Reference Manual
Example
Return an array sorted in ascending order:
Run Example »
Definition and Usage
The array_multisort() function returns a sorted array. You can input one or more arrays. The function sorts the first array first, then the others; if two or more values are equal, it sorts by the next array.
**Note:** String keys will be preserved, but numeric keys will be reindexed starting from 0 and increasing by 1.
**Note:** You can set sorting order and sorting type parameters after each array. If not set, each array parameter uses the default values.
Syntax
array_multisort(
array1,sorting order,sorting type,array2,array3...
)
| Parameters | Description |
|---|---|
| array1 | Required. Specifies the array. |
| sorting order | Optional. Specifies the sorting order. Possible values:
|
- SORT_REGULAR - Default. Compare items normally (Standard ASCII, do not change types).
- SORT_NUMERIC - Treat each item as a number.
- SORT_STRING - Treat each item as a string.
- SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed by setlocale() ).
- SORT_NATURAL - Treat each item as a string using natural ordering similar to natsort() natural ordering.
- SORT_FLAG_CASE - Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.
Technical Details
| Return Value: | Returns TRUE on success, FALSE on failure. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | The sorting types SORT_NATURAL and SORT_FLAG_CASE were added in PHP 5.4. |
排序类型 SORT_LOCALE_STRING 是在 PHP 5.3 中新增的。 |
More examples
Example 1
Return an array sorted in ascending order:
Run Example »
Example 2
How to sort when two values are equal:
Run Example »
Example 3
Using sorting parameters:
Run Example »
Example 4
Merge two arrays and sort them in descending numeric order:
Run Example »
Complete PHP Array Reference Manual