Php array descending order detailed tutorial

Source: Internet
Author: User
Generally, 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: & lt ;? Php $ learn learray (& amp; 39; name & amp; 39;, & amp; 39

Generally, 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. $ 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. In general, the ascending rule is used to compare numbers or strings:

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

Sorting of associated arrays in php:

In addition to numeric index arrays, php also supports related arrays. for example, the following array is a correlated array:

$ Doneles = array ('xm '=> 'name', 'xb' => 'sex', 'mz' => 'nation ', 'CS '=> 'birth ');

By default, sort ($ sort les) is sorted in ascending order based on the defined values of the elements. you can use the asort () function to represent the associated arrays, in the joined array, the most important thing is to sort by keywords (such as xm, xb, mz, etc.) in ascending order. This method uses the ksort () function.

  1. $ Doneles = array ('xm '=> 'name', 'xb' => 'sex', 'mz' => 'nation ', 'CS '=> 'birth ');
  2. Foreach ($ minutes Les as $ mychrs)
  3. Echo $ mychrs ."";
  4. Echo" -- Sort By element values in ascending order -- ";
  5. Asort ($ minutes les );
  6. Foreach ($ minutes Les as $ mychrs)
  7. Echo $ mychrs ."";
  8. Echo" -- Sort by keywords in ascending order -- ";
  9. Ksort ($ cmdles );
  10. Foreach ($ minutes Les as $ mychrs)
  11. Echo $ mychrs ."";
  12. ?>

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.