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 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:
- <?Php
- $Arr=Array(5=>"Zero ",3=>"One ",4=>"Two"); // defines an array
- Sort ($ arr); // sort the Array Using sort
- Foreach ($ arras $Key=>$ Value) // traverse the array to view the sorted result
- {
- Echo "Key: $ key; Value: $ value ;";
- }
- ?>
- <?Php
- // Define an array
- $Arr=Array(5=>"Zero ",3=>"One ",4=>"Two ");
- // Sort the Array Using rsort
- Rsort ($ arr );
- // Traverse the array to view the sorted result
- Foreach ($ arras $Key=>$ Value ){
- Echo "Key: $ key; Value: $ value ;";
- }
- ?>
- <?Php
- // Define two Arrays
- $Ar1=Array(5=>"Zero ",3=>"One ",4=>"Two ");
- $Ar2=Array(4=>"Three ",9=>"Four ",1=>"Five ");
- // Sort the Array
- Array_multisort ($ ar1, $ ar2 );
- // Traverse the array to view the sorted result
- Foreach ($ ar1as $Key=>$ Value ){
- Echo "Key: $ key; Value: $ value ;";
- }
- Echo"<BR>";
- Foreach ($ ar2as $Key=>$ Value ){
- Echo "Key: $ key; Value: $ value ;";
- }
- ?>
- <?Php
- // Define a two-dimensional array
- $ArrayArray= Array ("ar1" =>Array (5=>100,3=>120,4=>30 ),
- "Ar2" =>Array (4=>"Three ",9=>"Four ",1=>"Five "));
- // Sort the Array
- Array_multisort ($ array ["ar1"], SORT_NUMERIC, SORT_DESC,
- $ Array ["ar2"], SORT_STRING, SORT_ASC );
- // Traverse the array to view the sorted result
- Foreach ($ arrayas $ v1)
- {
- Foreach ($ v1as $ v2)
- {
- Echo "$ v2n ";
- }
- }
- ?>