array_multisort-to sort multiple arrays or multidimensional arrays
Description
BOOL Array_multisort (array ar1 [, mixed arg [, mixed ... [, array ...]] )
Array_multisort
(PHP 4, PHP 5)
Returns TRUE if successful, and FALSE if it fails.
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– Sorted in descending order
Sort Type flag:
sort_regular– Compare projects by usual method
sort_numeric– the project by numerical comparison
sort_string– to 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 that array-the default values SORT_ASC and Sort_regular before.
Example 1. To sort multiple arrays
$ar 1 = Array ("10″, +, +," 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 "10″," 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.
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)
}
Example 2. To sort a multidimensional array
$ar = Array ("10″, +, 1," a "), Array (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″,1 (sorted as numeric descent).
Example 3. Sorting multi-dimensional array
$ar = Array (
Array ("10″, 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 "10″,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).
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)
}
}
Example 4. To sort 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.
$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 example above 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 Rank
$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 example above 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.
Additional information:
The most complex sorting method for multidimensional array sorting in the PHP language. We will use PHP function Array_multisort () in the actual code to achieve this complex sort. For example, first sort a nested array with a common keyword, and then sort by another keyword. This is similar to sorting multiple fields with an order BY statement that uses SQL.
php function Asort () parse by the specific way of sorting values
Functional features of PHP function Arsort ()
Features of PHP natural language sorting
How to implement the reverse of natural language in PHP
How to implement a custom sort using PHP function Usort ()
The Listing J example shows us how the PHP function Array_multisort () works:
1, "name" = "Boney M", "rating" and "= 3", Array ("id" = 2, "name" = "Take That", "rating" and "= 1"), Array ("ID" = 3, "name" = "The Killers", "rating" and "4", Array ("id" = 4, "name" = "Lusain", "rating" + 3),); foreach ($data as $key + $value) {$name [$key] = $value [name]; $rating [$key] = $value [rating];} array_multisort ($rati Ng, $name, $data); Print_r ($data);? > Here, we simulate a row and column array in the $DATA array. I then rearrange the data collection using the PHP function Array_multisort (), starting with rating and then sorting by name if the rating is equal. It outputs the following results:
Copy CodeThe code is as follows:
Array ([0] = = Array
(
[id] = 2
[Name]
[Rating] = 1
) [1] = = Array
(
[id] = 1
[Name] = Boney M
[Rating] = 3
)
[2] = = Array
(
[id] = 4
[Name] = Lusain
[Rating] = 3
)
[3] = = Array
(
[id] = 3
[Name] = The Killers
[Rating] = 4
)
)
php function Array_multisort () is one of the most useful functions in PHP, and it has a very wide range of applications. Also, as you can see in the example, it is possible to sort multiple unrelated arrays, use one of them as the basis for the next sort, and sort the database result set.
http://www.bkjia.com/PHPjc/322689.html www.bkjia.com true http://www.bkjia.com/PHPjc/322689.html techarticle array_multisort-Ordering of multiple arrays or multidimensional arrays bool Array_multisort (array ar1 [, mixed arg [, mixed ... [, array ...]] ) Array_multisort (PHP 4, PHP 5) If ...