Notes on the use of the PHP array_multisort of function

  • 2020-05-09 18:20:07
  • OfStack

Function bool array_multisort (array &$arr [, mixed $arg = SORT_ASC [, mixed $arg = SORT_REGULAR [, mixed $...]])
The function sorts multiple arrays or multidimensional arrays
The first parameter is an array, and each subsequent parameter may be an array or the sort order flag below
SORT_ASC - default, in ascending order
SORT_DESC - in descending order
You can then specify the type of sort
SORT_REGULAR - default. Put each item in the normal order.
SORT_NUMERIC - put each item in numerical order.
SORT_STRING - put each item in alphabetical order.
The sample code
 
$arr1 = array('10', 11, 100, 100, 'a'); 
$arr2 = array(1, 2, 3, '2', 5); 
array_multisort($arr1, $arr2); 

The result is:
$arr1
Array ( [0] = > 10 [1] = > a [2] = > 11 [3] = > 100 [4] = > 100 )
# '10' converts to the integer 10 when compared to 11, 100, and 100, which is less than the other 3 Numbers
# '10' is used as a string when 'a' is compared, and the first character '1'ascii code value is 49 less than 'a' (ascii value is 97), so '10' is the smallest element
When # 'a' is compared with the other three Numbers, it is converted to the integer 0, which is less than the other three Numbers
$arr2
Array ( [0] = > 1 [1] = > 5 [2] = > 2 [3] = > 2 [4] = > 3 )
Element 1 of # $arr2 corresponds to element '10' of $arr1, so it is ranked in the [0] position
# $arr1[2] = > 100, $arr1[3] = > 100 corresponds to the $arr2 element 3, '2'. 3 is greater than '2', so $arr1[2] = corresponding to 2 > The index of 100 sorted is zero
3, corresponding to 3, $arr1[3] = > The index of 100 sort is 4
conclusion
1. The number of array elements participating in sorting remains 1
2. Sort array element position corresponding to, '10' = > 1 , 11 = > 2
3. The following array is sorted based on the order of the preceding array
4. The array in the front compares the array in the back if there are equal elements

array_multisort - sorts multiple arrays or multidimensional arrays

instructions
bool array_multisort ( array $ar1 [, mixed $arg [, mixed $... [, array $... ]]] )
Returns TRUE on success, or FALSE on failure.

array_multisort() can be used to sort multiple arrays at once, or multi-dimensional arrays by a dimension or dimension.

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

The input array is treated as the columns of a table and sorted by rows -- similar to the SQL ORDER BY clause. The first array is the primary array to sort. If the rows (values) in the array are the same, sort them 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 the sorting flags listed below.

Sorting order mark:

SORT_ASC - sorted in ascending order
SORT_DESC - sorted in descending order

Sorting type flag:

SORT_REGULAR - compares projects in the usual way
SORT_NUMERIC - compares items numerically
SORT_STRING - compares items to strings

You cannot specify two sort flags of the same kind after each array. The sort flag specified after each array is only valid for that array - previously the default values are SORT_ASC and SORT_REGULAR.

#1 sorts multiple arrays
 
<?php 
$ar1 = array("10", 100, 100, "a"); 
$ar2 = array(1, 3, "2", 1); 
array_multisort($ar1, $ar2); 
var_dump($ar1); 
var_dump($ar2); 
?> 

In this case, after sorting, the first array will contain "10", "a", 100,100. The second array will contain 1,1, "2", and 3. The order of the items in the second array is exactly the same as the order of the 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) 
} 

#2 sorts the multidimensional array
 
<?php 
$ar = array (array ("10", 100, 100, "a"), array (1, 3, "2", 1)); 
array_multisort ($ar[0], SORT_ASC, SORT_STRING, 
$ar[1], SORT_NUMERIC, SORT_DESC); 
?> 

After sorting in this example, the first array will contain 10,100,100, "a" (as a string ascending sort), and the second array will contain 1, 3, "2", and 1 (as a numeric descending sort).

#3 Sorting multi-dimensional array
 
<?php 
$ar = array( 
array("10", 11, 100, 100, "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 will become "10", 100,100,11, "a" (sorted as a string in ascending order). The second array will contain 1, 3, "2", 2, 1 (in descending order as Numbers).
 
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) 
} 
} 

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

The data in the example are as follows:

volume | edition
-------+--------
67 | 2
86 | 1
85 | 6
98 | 2
86 | 6
67 | 7

The data is all stored in an array called data. This is usually achieved by looping through the database, such as mysql_fetch_assoc().

< ?php
$data[] = array('volume' = > 67, 'edition' = > 2);
$data[] = array('volume' = > 86, 'edition' = > 1);
$data[] = array('volume' = > 85, 'edition' = > 6);
$data[] = array('volume' = > 98, 'edition' = > 2);
$data[] = array('volume' = > 86, 'edition' = > 6);
$data[] = array('volume' = > 67, 'edition' = > 7);
? >
In this example, volume will be sorted in descending order and edition in ascending order.

You now have an array with rows, but array_multisort() requires an array with columns, so use the following code to get the columns and sort them.

< ?php
// gets the list of columns
foreach ($data as $key = > $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}

// arrange the data in descending order according to volume and in ascending order according to edition
// take $data as the last parameter and sort 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 alphabetic sorting

Both SORT_STRING and SORT_REGULAR are case-sensitive, with uppercase letters coming before lowercase letters.

To do a case-insensitive sort, sort by copying the lowercase letters of the original array.
 
<?php 
$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 routine will output:

Array
(
[0] = > Alpha
[1] = > atomic
[2] = > bank
[3] = > Beta
)

To help you understand this useful function, take a look at the following example:


Example #6
 
<?php 
$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, 
//  Order the score from high to low as a numerical value  
$grade["name"], SORT_STRING, SORT_ASC); 
//  Sort the names as strings, from small to large  
var_dump($grade); 
?> 

The above routine 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 of grades, $grade, is sorted by score (score) from high to low, and people with the same score are sorted by name (name) from low to high. After sorting, li 4 was ranked first in 95, and zhao 6 60 in 5. No objection. Zhang 3. Wang 5 and liu 7 both scored 70 points, with Liu first, Wang last and Zhang last, in alphabetical order. To distinguish them, the three 70's are represented as integers, floating point Numbers, and strings, and their ordered results can be clearly seen in the program output.

Related articles: