PHP array sorting (ascending, descending, and related issues)

Source: Internet
Author: User
    1. $a = Array (4, "37", 3,100,0,-5);
    2. Sort ($a);
    3. for ($i =0; $i <6; + + $i) {
    4. echo $a [$i]. " ";
    5. }
    6. echo "
      ";
    7. Sort ($a, sort_string);
    8. for ($i =0; $i <6; + + $i) {
    9. echo $a [$i]. " ";
    10. }
    11. echo "
      ";
    12. ?>
Copy Code

Output Result:-5 0 3 4 37 100-5 0 100 3 37 4

Sort Descending: Rsort (array, [sort type]) parameter usage is the same as the sort function.

Associative array Ordering: functions: Asort (array, [Sort type]) Description: Sort Ascending According to the element values of the associative array. The parameters are used in the sort function above.

Function: Ksort (array, [Sort type]) Description: Sort Ascending According to the keyword of the associative array. The parameters are used in the sort function above.

    1. $a = Array (

    2. "Good" = "bad",
    3. "Right" = "wrong",
    4. "Boy" = "girl");

    5. echo "Value sort
      ";

    6. Asort ($a);
    7. foreach ($a as $key = = $value) {
    8. echo "$key: $value
      ";
    9. }

    10. echo "
      Key sort
      ";

    11. Ksort ($a);
    12. foreach ($a as $key = = $value) {
    13. echo "$key: $value
      ";
    14. }
    15. ?>

Copy Code

Output Result: Value sort Good:bad boy:girl right:wrong

Key sort Boy:girl Good:bad right:wrong descending sort: arsort (array, [sort type]) corresponds to Asort (array, [Sort type]) with Krsort Corresponding

Quickly create an array of functions range ()

For example, the range () function can quickly create arrays of numbers from 1 to 9:

    1. $numbers =range (1,9);
    2. echo $numbers [1];
    3. ?>
Copy Code

Of course, using range (9,1) creates an array of numbers from 9 to 1. Also, range () can create an array of characters from A to Z:

    1. $numbers =range (a,z);
    2. foreach ($numbers as $mychrs)
    3. echo $mychrs. " ";
    4. ?>
Copy Code

Note the case when using a character array, such as range (A,Z) and range (A,Z) are not the same. The range () function also has a third argument that sets the step size, such as the array elements created by range (1,9,3): 1, 4, 7. Common PHP array sorting the elements in a general array are represented by characters or numbers, so you can sort the elements in ascending order, the function is sort (). Like what:

    1. $people =array (' name ', ' Sex ', ' nation ', ' birth ');
    2. foreach ($people as $mychrs)
    3. echo $mychrs. " ";
    4. Sort ($people);
    5. echo "
      After---sort---
      ";
    6. foreach ($people as $mychrs)
    7. echo $mychrs. " ";
    8. ?>
Copy Code

An ascending sorted array element is displayed as birth name Nation sex, of course, the sort () function is case-sensitive (Letters from the largest to smallest order are: A ... Z...A...Z)

The sort () function also has a second parameter, which is used to indicate whether the rule in ascending order of the PHP array is to compare numbers or strings. Like what:

    1. echo "---sorted in ascending order of numbers---
      ";
    2. $num 2=array (' 26 ', ' 3 ',);
    3. Sort ($num 2,sort_numeric);
    4. foreach ($num 2 as $mychrs)
    5. echo $mychrs. " ";
    6. echo "
      ---ordered by word Fu Shen---
      ";
    7. $num 3=array (' 26 ', ' 3 ');
    8. Sort ($num 3,sort_string);
    9. foreach ($num 3 as $mychrs)
    10. echo $mychrs. " ";
    11. ?>
Copy Code

Sort_numeric and sort_string are used to declare in ascending order of numbers or characters. If the numbers are sorted in ascending order: 3, 26, but if ordered by word Fu Shen: 26, 3. In addition to the ascending function in PHP, there are functions in descending or reverse order, that is, the Rsort () function, such as: $num 1=range (1,9), Rsort ($num 1), which is actually equivalent to range (9,1).

The contents of the ordering of the PHP array are finished, I wish you a happy study.

>>> For more information, check out the PHP array sorting method Daquan <<<

  • Related Article

    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.