Returns TRUE on success or FALSE on failure.
array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions.
Associative (string) keys will be maintained, but numeric keys will be re-indexed.
The input arrays are treated as columns of a table to be sorted by rows - this resembles the functionality of SQL ORDER BY clause. The first array is the primary one to sort by. The rows (values) in that array that compare the same are sorted by the next input array, and so on.
The argument structure of this function is a bit unusual, but flexible. The first argument has to be an array. Subsequently, each argument can be either an array or a sorting flag from the following lists.
Sorting order flags:
SORT_ASC - Sort in ascending order
SORT_DESC - Sort in descending order
Sorting type flags:
SORT_REGULAR - Compare items normally
SORT_NUMERIC - Compare items numerically
SORT_STRING - Compare items as strings
No two sorting flags of the same type can be specified after each array. The sorting flags specified after an array argument apply only to that array - they are reset to default SORT_ASC and SORT_REGULAR before each new array argument.
Example 2. Sorting multi-dimensional array
In this example, after sorting, the first array will contain "10", 100, 100, "a" (it was sorted as strings in ascending order). The second will contain 1, 3, "2", 1 (sorted as numbers, in descending order).
|
Example 3. Sorting database results For this example, each element in the data array represents one row in a table. This type of dataset is typical of database records. Example data:
The data as an array, called data. This would usually, for example, be obtained by looping with mysql_fetch_assoc().
In this example, we will order by volume descending, edition ascending. We have an array of rows, but array_multisort() requires an array of columns, so we use the the below code to obtain the columns, then perform the sorting.
The dataset is now sorted, and will look like this:
|
Example 4. Case insensitive sorting Both SORT_STRING and SORT_REGULAR are case sensitive, strings starting with a capital letter will come before strings starting with a lowercase letter. To perform a case insensitve search, force the sorting order to be determined by a lowercase copy of the original array.
The above example will output:
|