Summary of common PHP array sorting methods

Source: Internet
Author: User

With the rapid development of PHP, more and more people are using it. In the PHP array learning excerpt, I learned about the establishment of the most basic PHP array and the display of array elements. You need to learn more about PHP Array Operations. The first issue is PHP array sorting and descending sorting.

Quickly create an array function range ()

 
 
  1. For example, the range () function can quickly create a number array from 1 to 9:
  2. <?Php 
  3. $Numbers=Range(1, 9 );
  4. Echo $ numbers [1];
  5. ?> 

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. <?php 
  2. $numbers=range(a,z);  
  3. foreach ($numbers as $mychrs)  
  4. echo $mychrs." ";  
  5. ?> 

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. <?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 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. <?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 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 ).


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.