Array_multisort implementation of the PHP multi dimensional array sorting example

  • 2020-03-31 21:28:18
  • OfStack

Array_multisort - sorts multiple arrays or multidimensional arrays
instructions
Bool array_multisort (array ar1 [, mixed arg [, mixed... [, array...]])

array_multisort
(PHP 4, PHP 5)
Returns TRUE on success and FALSE on failure.

Array_multisort () can be used to sort multiple arrays at once, or to sort multidimensional arrays by one or more dimensions.

The associated (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 BY rows, similar to the function of SQL's ORDER BY clause. The first array is the main 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 arguments can be an array or the sort flags listed below.

Sort order flag:
The SORT_ASC rows are sorted in ascending order

The SORT_DESC streams are sorted in descending order
Sort type flag:
SORT_REGULAR stream compares items in the usual way

SORT_NUMERIC compares items in terms of Numbers

The SORT_STRING stream compares items as 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 and is followed by the default values SORT_ASC and SORT_REGULAR.

Example 1. Sorting 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);
? >

After sorting in this example, the first array will contain "10," "a," 100,100. The second array will contain 1,1, "2", and 3. The items in the second array are in exactly the same order as the items in the first array (100 and 100).

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. Sorting multidimensional arrays

< ? 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" (sorted as the string goes up), and the second array will contain 1, 3, "2", and 1 (sorted as the value goes down).

Example 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)
}
}



Example 4. Sorting 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 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 called data. This is usually the result of a loop from a 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, we will order the volume in descending order and the edition in ascending order.

Now you have an array with rows, but array_multisort() needs an array with columns, so use the following code to get the columns and then sort them.

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

// sort the data by volume in descending order and by edition in ascending order
// take $data as the last parameter and sort by the general key
Array_multisort ($volume, SORT_DESC, $edition, SORT_ASC, $data);
? >

The data sets are 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, you sort by copying the lowercase letters of the original array.

< ? PHP
$array = array (' Alpha 'and' atomic ', 'Beta', 'bank');
$array_lowercase = array_map (' strtolower, $array);

Array_multisort ($array_lowercase SORT_ASC, SORT_STRING, $array);

Print_r ($array);
? >

The above example will output:

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



This function is quite useful, and to help you understand it, take a look at the following example:

Example 6. Ranking

< ? 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,
// sort the fractions from high to low as numerical values
$grade/" name ", SORT_STRING SORT_ASC);
// sort the names as strings from small to large
Var_dump ($grade);
? >

The above example 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 from high to low, and people with the same score are sorted by name from small to large. After sorting li si 95 into the first place, zhao liu 60 into the fifth place without objection. Zhang SAN, Wang wu and Liu qi all scored 70 points, and their names were ranked alphabetically, with Liu in front, Wang last and Zhang last. To differentiate, the three 70's are represented as integers, floating point Numbers, and strings, and their ordered results are clearly visible in the program output.
Supplementary information:
One of the most complex sorts for multidimensional arrays in the PHP language. We'll use the PHP function array_multisort() in the actual code to implement this complex sort. For example, a nested array is first sorted using a normal keyword, and then sorted by another keyword. This is very similar to using SQL's ORDER BY statement to sort multiple fields.
The PHP function asort() is parsed using a specific way of sorting values
PHP function arsort(
Introduction to the features of PHP natural language sorting
PHP natural language reverse order of the specific implementation
How to implement custom sort using the PHP function usort()
Listing J example illustrates how the PHP function array_multisort() works:
1, "name =" > "Boney M", "rating" = > 3), array (" id = "> 2, "name" = > "Take That," "rating" = > 1), array (" id = "> 3, "name =" > "The Killers", "rating" = > 4), array (" id = "> 4, "name =" > "Lusain", "rating" = > 3)); Foreach ($data as $key => $value) {$name[$key] = $value[name]; $rating value [rating] [$key] = $; $name} array_multisort ($rating, $data); Print_r ($data); ? > Here, we simulate a row and column array in the $data array. Then I use the PHP function array_multisort() to reorder the collection, first by rating and then, if the ratings are equal, by name. Its output is as follows:
 
Array ([0] => Array 
( 
[id] => 2 
[name] => Take That 
[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 
) 
) 

The PHP function array_multisort() is one of the most useful functions in PHP and has a wide range of applications. In addition, as you can see in the example, it can sort multiple unrelated arrays, use one of the elements as a basis for the next sort, and sort the database result set.

Related articles: