PHP Array_multisort () function Use notes _php tutorial

Source: Internet
Author: User
function bool Array_multisort (array & $arr [, Mixed $arg = Sort_asc [, Mixed $arg = Sort_regular [, mixed $ ...])
Parameter description: function to sort multiple arrays or multidimensional arrays
The first parameter is an array, each subsequent argument may be an array, or it may be the following sort order flag
SORT_ASC-By default, in ascending order
Sort_desc-Sorted in descending order
You can then specify the type of the sort
Sort_regular-Default. Arranges each item in a regular order.
Sort_numeric-Sorts each item in numerical order.
Sort_string-Arranges each item alphabetically.
Instance Code
Copy CodeThe code is as follows:
$arr 1 = Array (' A ', one, one, ten, ' a ');
$arr 2 = Array (1, 2, 3, ' 2 ', 5);
Array_multisort ($arr 1, $arr 2);

The result is:
$arr 1
Array ([0] = [1] = a [2] = = [3] = [4] = 100)
# ' 10 ' in comparison with 11, 100, 100 converts to an integer of 10, less than the other three numbers
# ' 10 ' is in ' a ' comparison as a string, its first character ' 1 ' ASCII value is 49 less than ' a ' (ASCII value is 97), so ' 10 ' is the smallest element
# ' A ' lies in the other three numbers when compared, converted to an integer 0, less than the other three numbers
$arr 2
Array ([0] = 1 [1] = 5 [2] = 2 [3] = 2 [4] = 3)
# $arr 2 element 1 corresponds to the position of the $ARR1 element ' 10 ', so it is at [0] position
# $arr 1[2] +, $arr 1[3] = 100 respectively corresponds to $ARR2 element 3, ' 2 '. 3 is greater than ' 2 ', so the $arr1[2 [corresponding to 2] = 100 is sorted by the subscript
3, with 3 corresponding to the $arr1[3] and 100 ranked subscript 4
Summarize
1. The number of array elements participating in the sorting remains the same
2. The position of the sorted array element is as follows, ' ten ' = 1, one and 2
3. The array in the back is sorted based on the order of the front array
4. The array in front is compared with an array of the same elements.

array_multisort-to sort multiple arrays or multidimensional arrays

Description
BOOL Array_multisort (array $ar 1 [, Mixed $arg [, mixed $ ... [, Array $ ...]] )
Returns TRUE on success, or FALSE on failure.

Array_multisort () can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional.

The association (string) key name remains the same, but the numeric key name is re-indexed.

The input array is treated as a column of a table and sorted in rows-this is similar to the function of the SQL ORDER by clause. The first array is the primary array to sort. The rows (values) in the array are compared to the same words, sorted by the size of the corresponding values in the next input array, and so on.

The parameter structure of this function is somewhat unusual, but very flexible. The first argument must be an array. Each of the following parameters can be an array or a sort flag listed below.

Sort order Flags:

Sort_asc-Sort by ascending order
Sort_desc-Sort by descending order

Sort Type flag:

Sort_regular-Compare items by usual method
Sort_numeric-Compare items by value
Sort_string-Compare items by string

You cannot specify two of the same sort flags after each array. The sort flags specified after each array are valid only for the array-before this is the default value Sort_asc and Sort_regular.

To sort multiple arrays #1
Copy CodeThe code is as follows:
$ar 1 = Array ("Ten", "N", "a");
$ar 2 = Array (1, 3, "2", 1);
Array_multisort ($ar 1, $ar 2);
Var_dump ($ar 1);
Var_dump ($ar 2);
?>

In this example, after sorting, the first array will contain "ten", "a", 100,100. The second array will contain 1, 1, "2", 3. The order of the items in the second array is exactly the same as the corresponding items (100 and 100) in the first array.
Copy CodeThe code is as follows:
Array (4) {
[0]=> string (2) "10"
[1]=> string (1) "a"
[2]=> Int (100)
[3]=> Int (100)
}
Array (4) {
[0]=> Int (1)
[1]=> Int (1)
[2]=> string (1) "2"
[3]=> Int (3)
}

To sort a multidimensional array #2
Copy CodeThe code is as follows:
$ar = Array (Array ("Ten", "+", "a"), Array (1, 3, "2", 1));
Array_multisort ($ar [0], SORT_ASC, sort_string,
$ar [1], sort_numeric, SORT_DESC);
?>

In this example, after sorting, the first array will contain 10,100,100, "a" (ascending sort as a string), and the second array will contain 1, 3, "2", and 1 (as a descending sort of a number).

#3 sorting multi-dimensional array
Copy CodeThe code is as follows:
$ar = Array (
Array ("Ten", one, one, one, "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 becomes "Ten", 100,100,11, "a" (as strings are sorted in ascending order). The second array will contain 1, 3, "2", 2, 1 (as a number in descending order).
Copy CodeThe code is as follows:
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)
}
}

#4 sorting the results of a database
In this example, each cell in the data array represents a row in a table. This is a data collection of typical database records.

The data in the example is as follows:

Volume | Edition
-------+--------
67 | 2
86 | 1
85 | 6
98 | 2
86 | 6
67 | 7

The data is all stored in an array named data. This is usually done by looping the results from the database, such as MYSQL_FETCH_ASSOC ().

$data [] = Array (' volume ' = +, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 1);
$data [] = Array (' volume ' = =, ' edition ' = 6);
$data [] = Array (' volume ' = = 98, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 6);
$data [] = Array (' volume ' = +, ' edition ' = 7);
?>
In this example, the volume is sorted in descending order, and the edition in ascending order.

Now that you have an array with rows, but Array_multisort () requires an array of columns, so use the following code to get the columns and sort them.

Get a list of columns
foreach ($data as $key = = $row) {
$volume [$key] = $row [' volume '];
$edition [$key] = $row [' Edition '];
}

Sort data in descending order according to volume, in ascending order of edition
The $data as the last parameter, sorted by the Universal key
Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data);
?>
The data set is now sorted and the results are as follows:

Volume | Edition
-------+--------
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7


Example #5 case-insensitive alphabetical sorting

Sort_string and Sort_regular are both case-sensitive letters, and uppercase letters precede lowercase letters.

To make a case-insensitive sort, sort by a lowercase copy of the original array.
Copy CodeThe code is as follows:
$array = Array (' Alpha ', ' Atomic ', ' Beta ', ' Bank ');
$array _lowercase = Array_map (' Strtolower ', $array);

Array_multisort ($array _lowercase, SORT_ASC, sort_string, $array);

Print_r ($array);
?>

The above routines will output:

Array
(
[0] = Alpha
[1] = Atomic
[2] = = Bank
[3] = Beta
)

This function is quite useful to help you understand, please look at the following example:


Example #6 Rank
Copy CodeThe code is as follows:
$grade = Array ("score" = = Array (70, 95, 70.0, 60, "70"),
"Name" = = Array ("Zhang San", "Li Si", "Wang Wu",
"Zhao Liu", "Liu Qi");
Array_multisort ($grade ["Score"], Sort_numeric, Sort_desc,
Sort fractions as numbers, from high to low
$grade ["Name"], sort_string, SORT_ASC);
Sort names as strings, from small to large
Var_dump ($grade);
?>

The above routines will output:

Array (2) {
["Score"]=>
Array (5) {
[0]=>
INT (95)
[1]=>
String (2) "70"
[2]=>
Float (70)
[3]=>
Int (70)
[4]=>
Int (60)
}
["Name"]=>
Array (5) {
[0]=>
String (5) "Li Si"
[1]=>
String (6) "Liu Qi"
[2]=>
String (7) "Wang Wu"
[3]=>
String (9) "Zhang San"
[4]=>
String (8) "Zhao Liu"
}
}
In this example, the array that contains the scores $grade sorted by fractions (score) from highest to lowest, and those with the same scores are sorted by first name (name) from small to large. After sorting Li 495 into the first place, Zhao 660 is divided into fifth place no objection. Zhang San, Harry and Liu Qi are all 70 points, their rankings by the alphabetical order of their names, Liu in front, Wang in the post and Zhang at the end. To differentiate, three 70 points are represented by integers, floating-point numbers, and strings, which can be clearly seen in the program output.

http://www.bkjia.com/PHPjc/323703.html www.bkjia.com true http://www.bkjia.com/PHPjc/323703.html techarticle the function bool Array_multisort (array $arr 2 = Array (1, 2, 3, ' 2 ', 5); Array_multisort ($arr 1, $arr 2); The result is: $arr 1 Array ([0] = ten [1] = a [2] = 11 [3] = 100 [4] = 100) # ' 10 ' in ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.