PHP array sorting by code _ PHP Tutorial

Source: Internet
Author: User
Code to sort PHP arrays. PHP has been developing for a long time and many users are familiar with PHP. here I will share my understanding of PHP array sorting and discuss it with you. PHP is an easy-to-learn and easy-to-use server. after a long period of development, many users know PHP very well. here I will share my understanding of PHP array sorting and discuss it with you. PHP is a server-side scripting language that is easy to learn and use. With little programming knowledge, you can use PHP to build a truly interactive WEB site. This tutorial does not want you to fully understand this language, but allows you to join the development of dynamic web sites as soon as possible. I suppose you have some basic HTML (or HTML editor) knowledge and some programming ideas.

PHP webpage files are processed as common HTML webpage files. during editing, you can use the conventional HTML editing method to compile PHP. PHP stands for Hypertext Preprocessor (PHP: Hypertext Preprocessor ). PHP is completely free and free to spend, you can download from PHP official site (http://www.php.net) freely. PHP complies with the GNU Public License (GPL), under which many popular software such as Linux and Emacs are born. You can obtain the source code without restrictions, or even add the features you need. PHP can run on most Unix platforms, GUN, Linux, and Microsoft Windows. After talking about this, let's explain how to sort PHP arrays.

PHP array sorting code:

 
 
  1. Php
  2. $Arr=Array(5=>"Zero ",3=>"One ",4=>"Two"); // defines an array
  3. Sort ($ arr); // sort the array using sort
  4. Foreach ($ arras $Key=>$ Value) // traverse the array to view the sorted result
  5. {
  6. Echo "Key: $ key; Value: $ value ;";
  7. }
  8. ?>
  9. Php
  10. // Define an array
  11. $Arr=Array(5=>"Zero ",3=>"One ",4=>"Two ");
  12. // Sort the array using rsort
  13. Rsort ($ arr );
  14. // Traverse the array to view the sorted result
  15. Foreach ($ arras $Key=>$ Value ){
  16. Echo "Key: $ key; Value: $ value ;";
  17. }
  18. ?>
  19. Php
  20. // Define two arrays
  21. $Ar1=Array(5=>"Zero ",3=>"One ",4=>"Two ");
  22. $Ar2=Array(4=>"Three ",9=>"Four ",1=>"Five ");
  23. // Sort the array
  24. Array_multisort ($ ar1, $ ar2 );
  25. // Traverse the array to view the sorted result
  26. Foreach ($ ar1as $Key=>$ Value ){
  27. Echo "Key: $ key; Value: $ value ;";
  28. }
  29. Echo"<BR>";
  30. Foreach ($ ar2as $Key=>$ Value ){
  31. Echo "Key: $ key; Value: $ value ;";
  32. }
  33. ?>
  34. Php
  35. // Define a two-dimensional array
  36. $ArrayArray= Array ("ar1" =>Array (5=>100,3=>120,4=>30 ),
  37. "Ar2" =>Array (4=>"Three ",9=>"Four ",1=>"Five "));
  38. // Sort the array
  39. Array_multisort ($ array ["ar1"], SORT_NUMERIC, SORT_DESC,
  40. $ Array ["ar2"], SORT_STRING, SORT_ASC );
  41. // Traverse the array to view the sorted result
  42. Foreach ($ arrayas $ v1)
  43. {
  44. Foreach ($ v1as $ v2)
  45. {
  46. Echo "$ v2n ";
  47. }
  48. }
  49. ?>


Bytes. PHP is a server that is easy to learn and use...

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.