Php array sorting (ascending, descending and related issues)

Source: Internet
Author: User
Php array sorting (ascending, descending and related issues)

  1. $ A = array (4, "37", 3,100, 0,-5 );
  2. Sort ($ );
  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. ?>

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

Sort in descending order: rsort (array, [sort type]) parameters are used in the same way as sort functions.

Sort joined arrays: Function: asort (array, [sort type]) description: sort joined arrays in ascending order. See the above sort function for parameters.

Function: ksort (array, [sort type]) description: sort in ascending order based on the keywords of the associated array. See the above sort function for parameters.

  1. $ A = array (

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

  5. Echo "value sort
    ";

  6. Asort ($ );
  7. Foreach ($ a as $ key => $ value ){
  8. Echo "$ key: $ value
    ";
  9. }

  10. Echo"
    Key sort
    ";

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

Output result: value sort good: bad boy: girl right: wrong

Key sort boy: girl good: bad right: wrong sort in descending order: arsort (array, [sort type]) corresponds to krsort (array, [sort type]) and ksort

Quickly create an array function range ()

For example, the range () function can quickly create a number array from 1 to 9:

  1. $ Numbers = range (1, 9 );
  2. Echo $ numbers [1];
  3. ?>

Of course, range (9, 1) is used to create a number array from 9 to 1. At the same time, range () can also create a character array from a to z:

  1. $ Numbers = range (a, z );
  2. Foreach ($ numbers as $ mychrs)
  3. Echo $ mychrs ."";
  4. ?>

Note the case sensitivity when using character arrays. for example, range (A, z) and range (a, Z) are different. The range () function also has a third parameter, which is used to set the step size. for example, the array elements created by range (, 3) are: 1, 4, and 7. Common PHP array sorting: elements in an array are represented by characters or numbers. Therefore, you can sort array elements in ascending order. This function is sort (). For example:

  1. $ People = array ('name', 'sex', 'nation', 'birth ');
  2. Foreach ($ people as $ mychrs)
  3. Echo $ mychrs ."";
  4. Sort ($ people );
  5. Echo"
    --- After sorting ---
    ";
  6. Foreach ($ people as $ mychrs)
  7. Echo $ mychrs ."";
  8. ?>

The array elements in ascending order are displayed as birth name nation sex. of course, the sort () function is case-sensitive (the order of letters from large to small is:... Z... A... Z)

The Sort () function also has a second parameter, which indicates whether the PHP array sorting ascending rule is used to compare numbers or strings. For example:

  1. Echo "--- sort by numbers in ascending order ---
    ";
  2. $ Num2 = array ('26', '3 ',);
  3. Sort ($ num2, SORT_NUMERIC );
  4. Foreach ($ num2 as $ mychrs)
  5. Echo $ mychrs ."";
  6. Echo"
    --- Sort by character in ascending order ---
    ";
  7. $ Num3 = array ('26', '3 ');
  8. Sort ($ num3, SORT_STRING );
  9. Foreach ($ num3 as $ mychrs)
  10. Echo $ mychrs ."";
  11. ?>

SORT_NUMERIC and SORT_STRING are used to declare the ascending order of numbers or characters. If the numbers are listed in ascending order: 3, 26, but if the characters are listed in ascending order, they are listed as 26, 3. In PHP, in addition to the ascending function, there are also descending or inverse sort functions, which are rsort () functions, such as: $ num1 = range (); rsort ($ num1 ); this is actually equivalent to range (9, 1 ).

I have finished the introduction of php array sorting and wish you a pleasant learning experience.

>>> For more information, see The php array sorting method <

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.