array_multisort
Sort multiple or multi-dimensional arrays
&reftitle.description;
boolarray_multisort
arrayarr
mixedargSORT_ASC
mixedargSORT_REGULAR
mixed...
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.
&reftitle.parameters;
arr
An array being sorted.
arg
Optionally another array, or sort options for the
previous array argument:
Sorting type flags:
SORT_ASC - sort items ascendingly.
SORT_DESC - sort items descendingly.
SORT_REGULAR - compare items normally
(don't change types)
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as
strings, based on the current locale. It uses the locale,
which can be changed using setlocale
SORT_NATURAL - compare items as strings
using "natural ordering" like natsort
SORT_FLAG_CASE - can be combined
(bitwise OR) with
SORT_STRING or
SORT_NATURAL to sort strings case-insensitively
...
Additional arg's.
&reftitle.returnvalues;
&return.success;
&reftitle.changelog;
&Version;
&Description;
5.4.0
The SORT_NATURAL and SORT_FLAG_CASE
were added to arg as possible sort flags.
5.3.0
The SORT_LOCALE_STRING was added to
arg as possible sort flags.
&reftitle.examples;
Sorting multiple arrays
]]>
In this example, after sorting, the first array will contain 0,
10, 100, 100. The second array will contain 4, 1, 2, 3. The
entries in the second array corresponding to the identical
entries in the first array (100 and 100) were sorted as well.
int(0)
[1]=> int(10)
[2]=> int(100)
[3]=> int(100)
}
array(4) {
[0]=> int(4)
[1]=> int(1)
[2]=> int(2)
[3]=> int(3)
}
]]>
Sorting multi-dimensional array
]]>
In this example, after sorting, the first array will transform to
"10", 100, 100, 11, "a" (it was sorted as strings in ascending
order). The second will contain 1, 3, "2", 2, 1 (sorted as numbers,
in descending order).
array(5) {
[0]=> string(2) "10"
[1]=> int(100)
[2]=> int(100)
[3]=> int(11)
[4]=> string(1) "a"
}
[1]=> array(5) {
[0]=> int(1)
[1]=> int(3)
[2]=> string(1) "2"
[3]=> int(2)
[4]=> int(1)
}
}
]]>
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.
67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);
?>
]]>
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 below code to obtain the
columns, then perform the sorting.
$row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>
]]>
The dataset is now sorted, and will look like this:
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 insensitive search, force the sorting order to be
determined by a lowercase copy of the original array.
]]>
&example.outputs;
Alpha
[1] => atomic
[2] => bank
[3] => Beta
)
]]>
&reftitle.seealso;
usort
&seealso.array.sorting;