Php array sorting methods

Source: Internet
Author: 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 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:

  1. $ Ages = array (
  2. 'C' => 'php ',
  3. 'D' => 'asp ',
  4. 'A' => 'JSP ',
  5. 'B' => 'Java'
  6. );
  7. Krsort ($ ages );
  8. Foreach ($ ages as $ key => $ val ){
  9. Echo "$ key = $ val ".'
    ';
  10. };
  11. ?>

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:

  1. Asort ($ ages );
  2. Print_r ($ ages );
  3. Echo"
    ";
  4. Rsort ($ ages );
  5. 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:

  1. Sort ($ ages );
  2. Foreach ($ ages as $ key => $ val ){
  3. Echo "ages [$ key] = $ val ".'
    ';
  4. };

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:

  1. $ Ar1 = array ("ten", 100,100, "");
  2. $ Ar2 = array (1, 3, "2", 1 );
  3. Array_multisort ($ ar1, $ ar2 );
  4. Var_dump ($ ar1 );
  5. Var_dump ($ ar2 );
  6. ?>

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:

  1. Array (4 ){
  2. [0] => string (2) "10"
  3. [1] => string (1) ""
  4. [2] => int (100)
  5. [3] => int (100)
  6. }
  7. Array (4 ){
  8. [0] => int (1)
  9. [1] => int (1)
  10. [2] => string (1) "2"
  11. [3] => int (3)
  12. }

Example 2: sort multidimensional arrays,The code is as follows:

  1. $ Ar = array ("10", 100,100, "a"), array (1, 3, "2", 1 ));
  2. Array_multisort ($ ar [0], SORT_ASC, SORT_STRING,
  3. $ Ar [1], SORT_NUMERIC, SORT_DESC );
  4. ?>
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:

  1.  
  2. $ Aa = array ("score" => 60 ),
  3. Array ("score" => 70 ),
  4. Array ("score" => 80 ),
  5. Array ("score" => 90 ),
  6. Array ("score" => 20 ),
  7. Array ("score" => 10 ),
  8. Array ("score" => 50 ),
  9. Array ("score" => 30 ));
  10.  
  11. Echo '------ output before sorting ------ ';
  12. Var_dump ($ aa); // output before sorting
  13.  
  14. Usort ($ aa, "cmp"); // sort (from large to small)
  15.  
  16. Echo' ------ Output after sorting ------ ';
  17. Var_dump ($ aa); // sorted output
  18.  
  19. /**
  20. * Custom sorting conditions
  21. * @ Param array $
  22. * @ Param array $ B
  23. * @ Return bool
  24. */
  25. Function cmp ($ a, $ B ){
  26. If ($ a ["score"] = $ B ["score"]) {
  27. Return 0;
  28. }
  29. Return ($ a ["score"] <$ B ["score"])? 1:-1;
  30. }
  31.  
  32. ?>

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

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.