array_multisortDescriptionThis 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: WarningThis function directly alters all arrays passed to it. See alsoSee also arsort() , asort() , krsort() , ksort() , natsort() , natcasesort() , rsort() , sort() , uasort() , uksort() , usort() Example$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 ) Example 25. 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 )
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|