array_multisort
bool array_multisort(array array_1, [mixed ...])
|
array_1
|
Array to sort
|
|
...
|
Further arrays to sort, or sorting flags
|
Sorts one or more arrays.
Returns:
TRUE on success; FALSE on failure
Description:
This function sorts one or more arrays at a time, while keeping key/value relationships intact. The sorting is done as though the arrays were columns in a table. The first array is sorted, and identical elements are then sorted by the corresponding elements from the second array. Identical elements of the second array are then sorted by the corresponding elements from the third array, and so on. This example assumes that you have at least three arrays, of course.
After the first argument, array_1
, which must be an array, any argument can be either another array to sort or one of the flags from the following list. Any flags apply only to the previous array argument, and only one flag of each type can apply to a given array. Therefore, you should never wind up with more than two flag arguments after an array argument.
The flags are represented by named constants; there are two types of flags, as described in the following paragraphs.
One type of flag controls the order by which to sort the arrays:
Another flag type controls how elements are compared:
SORT_REGULAR: Sort according to the normal PHP rules for comparison. This is the default if not specified for a particular array.
SORT_NUMERIC: Sort by comparing elements as numbers.
SORT_STRING: Sort by comparing elements as strings.
Warning:
This function directly alters all arrays passed to it.
Version:
PHP 4 since 4.0b4
See also:
arsort()
asort()
krsort()
ksort()
natsort()
natcasesort()
rsort()
sort()
uasort()
uksort()
usort()
Examples:
Sort two arrays
$array_1 = array( 'one' => 'Dave',
'two' => 'Piia',
'three' => 'Leigh',
'four' => 'Adam',
'five' => 'Leigh');
$array_2 = array( 'one' => 'Derrick',
'two' => 'Sarah',
'three' => 'Morgan',
'four' => 'Laura',
'five' => 'Anoosh');
$retval = array_multisort($array_1, $array_2);
print_r($array_1);
print_r($array_2);
Output:Array
(
[four] => Adam
[one] => Dave
[five] => Leigh
[three] => Leigh
[two] => Piia
)
Array
(
[four] => Laura
[one] => Derrick
[five] => Anoosh
[three] => Morgan
[two] => Sarah
)
Sort two arrays in descending order
$array_1 = array( 'one' => 'Dave',
'two' => 'Piia',
'three' => 'Leigh',
'four' => 'Adam',
'five' => 'Leigh');
$array_2 = array( 'one' => 'Derrick',
'two' => 'Sarah',
'three' => 'Morgan',
'four' => 'Laura',
'five' => 'Anoosh');
$retval = array_multisort($array_1, $array_2, SORT_DESC);
print_r($array_1);
print_r($array_2);
Output:
Array
(
[four] => Adam
[one] => Dave
[three] => Leigh
[five] => Leigh
[two] => Piia
)
Array
(
[four] => Laura
[one] => Derrick
[three] => Morgan
[five] => Anoosh
[two] => Sarah
)
|