Summary of common PHP array sorting methods _php Tutorial

Source: Internet
Author: User
With the rapid development of PHP, more and more people with it, in the PHP array learning excerpt part of the basic PHP array to understand the establishment and the display of the group elements. Need to learn more about the operation of the PHP array. The first contact is the PHP array sorting, descending sort problem.

Quickly create an array of functions range ()

 
  
  
  1. For example, the range () function can quickly create arrays of numbers from 1 to 9:
  2. Php
  3. $ numbers = Range (1,9);
  4. echo $numbers [1];
  5. ?>

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

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. php
  2. $ people = Array (' name ', ' Sex ', ' nation ', ' birth ');
  3. foreach ($people as $mychrs)
  4. echo $mychrs. " ";
  5. Sort ($people);
  6. echo " <BR />---sorted---<br /> ";
  7. foreach ($people as $mychrs)
  8. echo $mychrs. " ";
  9. ?>

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. php
  2. echo "---sorted in ascending order of numbers--- <BR />";
  3. $ num2 = Array (' 26 ', ' 3 ',);
  4. Sort ($num 2,sort_numeric);
  5. foreach ($num 2 as $mychrs)
  6. echo $mychrs. " ";
  7. echo " <BR />---sorted by word Fu Shen---<br /> ";
  8. $ num3 = Array (' 26 ', ' 3 ');
  9. Sort ($num 3,sort_string);
  10. foreach ($num 3 as $mychrs)
  11. echo $mychrs. " ";
  12. ?>

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


http://www.bkjia.com/PHPjc/446476.html www.bkjia.com true http://www.bkjia.com/PHPjc/446476.html techarticle with the rapid development of PHP, more and more people with it, in the PHP array learning excerpt part of the basic PHP array to understand the establishment and the display of the group elements. Need to drill down into the PHP 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.