In php, there are many array sorting functions. next I will give you a summary of some of our commonly used array sorting functions and the most commonly used functions of array sorting instances written by the user.
In php, there are many array sorting functions. next I will give you a summary of some of our commonly used array sorting functions and your own array sorting instances.
The most common function of array sorting is sort ($ arr). it is used to sort the key values of an array in ascending order, and the sorted array key name is no longer the original key name, is the key name reset by the new array.
Sometimes we need more complex sorting. For example, to sort key names, ksort ($ arr) is used here. it sorts the keys of the array and maintains the original key-value relationship. The corresponding asort ($ arr) function is used to sort key values and maintain the original key-value relationship.
In the same principle, rsort (); arsort (); krsort (); functions are sorted in descending order, and other functions include sort (); rsort (); ksort (); same.
Array operations are an important foundation for PHP and we hope to make good use of them.
Sort key values, that is, sort by the size and order of the ASC Ⅱ value.
Ksort (): Sort by array identifier
Krsort (): Sort by array identifier in reverse order
Instance 1, the code is as follows:
-
- $ Ages = array (
- 'C' => 'php ',
- 'D' => 'asp ',
- 'A' => 'JSP ',
- 'B' => 'Java'
- );
- Krsort ($ ages );
- Foreach ($ ages as $ key => $ val ){
- Echo "$ key = $ val ".'
';
- };
- ?>
Sort by element value
Asort (): sorts arrays in ascending order;
Rsort (): sort the array in descending order.
Change the line 8-11 of instance 1:
- Asort ($ ages );
- Print_r ($ ages );
- Echo"
";
- Rsort ($ ages );
- Print_r ($ ages );
Delete the sorting of original key names
Sort (): sorts arrays in ascending order;
Rsort (): sort the array in descending order.
Change the 8-11 line code of instance 2:
- Sort ($ ages );
- Foreach ($ ages as $ key => $ val ){
- Echo "ages [$ key] = $ val ".'
';
- };
Array_multisort -- sorts multiple arrays or multi-dimensional arrays.
Note:Bool array_multisort (array ar1 [, mixed arg [, mixed... [, array...])
Example 1. sort multiple arrays,The code is as follows:
-
- $ Ar1 = array ("ten", 100,100, "");
- $ Ar2 = array (1, 3, "2", 1 );
- Array_multisort ($ ar1, $ ar2 );
- Var_dump ($ ar1 );
- Var_dump ($ ar2 );
- ?>
In this example, after sorting, the first array will contain "10", "a", 100,100. The second array will contain 1, 1, "2", 3. The project order in the second array is exactly the same as that in the first array (100 and 100). The code is as follows:
- Array (4 ){
- [0] => string (2) "10"
- [1] => string (1) ""
- [2] => int (100)
- [3] => int (100)
- }
- Array (4 ){
- [0] => int (1)
- [1] => int (1)
- [2] => string (1) "2"
- [3] => int (3)
- }
Example 2: sort multidimensional arrays,The code is as follows:
-
- $ Ar = array ("10", 100,100, "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" (as the string ascending order), and the second array will contain 1, 3, "2 ", 1 (sort by numerical descent)
PHP array sorting functions are very powerful, such as arsort, asort, krsort, ksort, sort ..., But sometimes it still cannot meet our needs. if there is a two-dimensional array, we need to sort by a value in the two-dimensional array, we need to use the usort custom array sorting. The example is as follows:
-
-
- $ Aa = array ("score" => 60 ),
- Array ("score" => 70 ),
- Array ("score" => 80 ),
- Array ("score" => 90 ),
- Array ("score" => 20 ),
- Array ("score" => 10 ),
- Array ("score" => 50 ),
- Array ("score" => 30 ));
-
- Echo '------ output before sorting ------ ';
- Var_dump ($ aa); // output before sorting
-
- Usort ($ aa, "cmp"); // sort (from large to small)
-
- Echo' ------ Output after sorting ------ ';
- Var_dump ($ aa); // sorted output
-
- /**
- * Custom sorting conditions
- * @ Param array $
- * @ Param array $ B
- * @ Return bool
- */
- Function cmp ($ a, $ B ){
- If ($ a ["score"] = $ B ["score"]) {
- Return 0;
- }
- Return ($ a ["score"] <$ B ["score"])? 1:-1;
- }
-
- ?>
Output result:
------ Output before sorting ------
Array (8) {[0] => array (1) {["score"] => int (60)} [1] => array (1) {["score"] => int (70)} [2] => array (1) {["score"] => int (80 )} [3] => array (1) {["score"] => int (90)} [4] => array (1) {["score"] => int (20)} [5] => array (1) {["score"] => int (10 )} [6] => array (1) {["score"] => int (50)} [7] => array (1) {["score"] => int (30 )}}
------ Output after sorting ------
Array (8) {[0] => array (1) {["score"] => int (90)} [1] => array (1) {["score"] => int (80)} [2] => array (1) {["score"] => int (70 )} [3] => array (1) {["score"] => int (60)} [4] => array (1) {["score"] => int (50)} [5] => array (1) {["score"] => int (30 )} [6] => array (1) {["score"] => int (20)} [7] => array (1) {["score"] => int (10 )}}