array_multisort
array_multisort
Sort multiple or multi-dimensional arrays
true **array_multisort** array $array1 mixed $array1_sort_order mixed $array1_sort_flags mixed $rest
can be used to sort several
arrays at once, or a multi-dimensional array by one or more
dimensions.
array_multisort
Associative () keys will be maintained, but numeric
keys will be re-indexed.
string
array1
An being sorted.
array
array1_sort_order
The order used to sort the previous argument. Either
to sort ascendingly or
to sort descendingly.
array``SORT_ASC``SORT_DESC
This argument can be swapped with
or omitted entirely, in which case is assumed.
`array1_sort_flags``SORT_ASC`
array1_sort_flags
Sort options for the previous argument:
array
Sorting type flags:
- - compare items normally (don't change types)`SORT_REGULAR`
-
- compare items numerically
SORT_NUMERIC
- compare items numerically
-
- compare items as strings
SORT_STRING
- compare items as strings
-
- compare items as strings, based on the current locale. It uses the locale, which can be changed using
SORT_LOCALE_STRING``setlocale
- compare items as strings, based on the current locale. It uses the locale, which can be changed using
-
- compare items as strings using "natural ordering" like
SORT_NATURAL``natsort
- compare items as strings using "natural ordering" like
-
-
can be combined (bitwise OR) with or to sort strings case-insensitively
SORT_FLAG_CASE``SORT_STRING``SORT_NATURALThis argument can be swapped with or omitted entirely, in which case is assumed.
array1_sort_order``SORT_REGULAR
-
restMore arrays, optionally followed by sort order and flags. Only elements
corresponding to equivalent elements in previous arrays are compared.
In other words, the sort is lexicographical.
return.true.always
Voorbeeld: Sorting multiple arrays
<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);
?>
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.
array(4) {
[0]=> 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)
}
Voorbeeld: Sorting multi-dimensional array
<?php
$ar = array(
array("10", 11, 100, 100, "a"),
array( 1, 2, "2", 3, 1)
);
array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>
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(2) {
[0]=> 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)
}
}
Voorbeeld: Sorting database results
For this example, each element in the
array represents one row in a table. This type of dataset is typical
of database records.
`data`
Example data:
volume | edition
-------+--------
67 | 2
86 | 1
85 | 6
98 | 2
86 | 6
67 | 7
The data as an array, called . This would usually,
for example, be obtained by looping with .
`data``mysqli_fetch_assoc`
In this example, we will order by descending,
ascending.
`volume``edition`
We have an array of rows, but
requires an array of columns, so we use the below code to obtain the
columns, then perform the sorting.
`array_multisort`
<?php
// The data as created by looping over mysqli_fetch_assoc:
$data[] = array('volume' => 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);
// Obtain a list of columns
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}
// You can use array_column() instead of the above code
$volume = array_column($data, 'volume');
$edition = array_column($data, '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);
// Loop over the data and output the sorted values for each column
echo 'volume | edition', PHP_EOL;
echo '-------+--------', PHP_EOL;
for ($i = 0; $i < count($data); $i++) {
printf("%6d | %7d\n", $volume[$i], $edition[$i]);
}
?>
The dataset is now sorted, and will look like this:
volume | edition
-------+--------
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7
Voorbeeld: Case insensitive sorting
Both and
are case sensitive, strings
starting with a capital letter will come before strings starting
with a lowercase letter.
`SORT_STRING``SORT_REGULAR`
To perform a case insensitive sort, force the sorting order to be determined by a lowercase copy of the original array.
<?php
$array = array('Alpha', 'atomic', 'Beta', 'bank');
$array_lowercase = array_map('strtolower', $array);
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array);
print_r($array);
?>
Array
(
[0] => Alpha
[1] => atomic
[2] => bank
[3] => Beta
)
usort