php function sort () How to implement array sorting _php tutorial

Source: Internet
Author: User
We are learningThe elements in a general array are represented by characters or numbers, so you can arrange the elements in ascending order, which is the sort () function. 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. ?>

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

The Sort () function also has a second argument that indicates whether the ascending rule is used 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 PHP function sort (), for example:

 
  
  1. $ NUM1 = Range (1,9);

http://www.bkjia.com/PHPjc/446405.html www.bkjia.com true http://www.bkjia.com/PHPjc/446405.html techarticle We are learning that each element in a general array is represented by a character or number, so it is possible to arrange the elements in ascending order, which is the sort () function. Like what:? 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.