PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




array_multisort

Name

array_multisort — Sorts one or more arrays.

Synopsis

bool array_multisort(array_1[, . . .]);
array array_1: Array to sort
mixed . . . (optional): Further arrays to sort, or sorting flags

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:

  • SORT_ASC: Sort the arrays in ascending order. This is the default if not specified for a particular array.

  • SORT_DESC: Sort the arrays in descending order.



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

Example

Example 24. 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
)

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.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.