How to sort arrays using the PHP function sort () _ PHP Tutorial

Source: Internet
Author: User
How does PHP function sort () sort arrays. In general, the elements in an array are represented by characters or numbers. Therefore, you can sort the array elements in ascending order. This function is sort (). For example :? Php $ learn learray we are learningGenerally, all elements in an array are represented by characters or numbers. Therefore, you can sort the array elements in ascending order. This function is sort (). For example:

 
 
  1. Php
  2. $People=Array('Name', 'sex', 'nation', 'birth ');
  3. Foreach ($ people as $ mychrs)
  4. Echo $ mychrs ."";
  5. Sort ($ people );
  6. Echo"<Br />--- After sorting ---<Br />";
  7. Foreach ($ people as $ mychrs)
  8. Echo $ mychrs ."";
  9. ?>

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

The Sort () function also has a second parameter. the ascending rule is used to compare numbers and strings. For example:

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


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 addition to the ascending function, PHP also has a descending or reverse sort function, which is the PHP function sort (). For example:

 
 
  1. $Num1=Range(1, 9 );
  2. Rsort ($ num1); // here it is actually equivalent to range (9, 1)

Elements in an array are represented by characters or numbers. Therefore, you can sort the array elements in ascending order. This function is sort (). For example :? Php $ people = array...

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.