Parses the functions of ascending, descending, and re-sorting of array elements in PHP.

Source: Internet
Author: User
Tags shuffle
This article provides a detailed analysis of the functions of array elements in PHP in ascending, descending, and re-sorting. For more information, see 1. quickly create an array function range ()
For example, the range () function can quickly create a number array from 1 to 9:

The code is as follows:


$ Numbers = range (1, 9 );
Echo $ numbers [1];
?>


Of course, range (9, 1) is used to create a number array from 9 to 1. At the same time, range () can also create a character array from a to z:

The code is as follows:


$ Numbers = range (a, z );
Foreach ($ numbers as $ mychrs)
Echo $ mychrs ."";
?>


Note the case sensitivity when using character arrays. for example, range (A, z) and range (a, Z) are different.
The range () function also has a third parameter, which is used to set the step size. for example, the array elements created by range (, 3) are: 1, 4, and 7.

2. sorting regular arrays in PHP
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:

The code is as follows:


$ People = array ('name', 'sex', 'nation', 'birth ');
Foreach ($ people as $ mychrs)
Echo $ mychrs ."";
Sort ($ people );
Echo"
--- After sorting ---
";
Foreach ($ people as $ mychrs)
Echo $ mychrs ."";
?>


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. the ascending rule is used to compare numbers and strings. For example:

The code is as follows:


Echo "--- sort by numbers in ascending order ---
";
$ Num2 = array ('26', '3 ',);
Sort ($ num2, SORT_NUMERIC );
Foreach ($ num2 as $ mychrs)
Echo $ mychrs ."";
Echo"
--- Sort by character in ascending order ---
";
$ Num3 = array ('26', '3 ');
Sort ($ num3, SORT_STRING );
Foreach ($ num3 as $ mychrs)
Echo $ mychrs ."";
?>


SORT_NUMERIC and SORT_STRING are used to declare the ascending order of numbers or characters. If the numbers are listed in ascending order: 3, 26, but if the characters are listed in ascending order, they are listed as 26, 3.
In PHP, in addition to the ascending function, there are also descending or reverse sort functions, which are rsort () functions, such:

The code is as follows:


$ Num1 = range (1, 9 );
Rsort ($ num1 );


This is actually equivalent to range (9, 1)

3. 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:

The code is as follows:


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

The code is as follows:


$ Doneles = array ('xm '=> 'name', 'xb' => 'sex', 'mz' => 'nation ', 'CS '=> 'birth ');
Foreach ($ minutes Les as $ mychrs)
Echo $ mychrs ."";
Echo"
-- Sort By element values in ascending order --
";
Asort ($ minutes les );
Foreach ($ minutes Les as $ mychrs)
Echo $ mychrs ."";
Echo"
-- Sort by keywords in ascending order --
";
Ksort ($ cmdles );
Foreach ($ minutes Les as $ mychrs)
Echo $ mychrs ."";
?>


Corresponds to the reverse sort rsort () descending function of the sort () ascending function of a conventional array, and the associated array also has the corresponding descending function: asort () functions: arsort (), ksort (), and krsort.
Memory: The prototype function is sort (), where a and k indicate that the correlated array must be prefixed, and r is used for reverse sorting.

4. Random Sorting of PHP array elements
In PHP, the shuffle () function is used to randomly sort array elements. for example:

The code is as follows:


$ Fer = array ('cnbruce ', 'cnrose', 'cnjames ', 'cnanne ');
Shuffle ($ fer );
Foreach ($ fer as $ mychrs)
Echo $ mychrs ."";
?>


Different sorting combinations are displayed each time.

5. PHP array in reverse order
In PHP, the array_reverse () function can be used to sort array elements in reverse order. For example:

The code is as follows:


$ Fer = array ('cnbruce ', 'cnrose', 'cnjames ', 'cnanne ');
Foreach ($ fer as $ mychrs)
Echo $ mychrs ."";
$ Fer = array_reverse ($ fer );
Echo"
-- Reverse in the original order --
";
Foreach ($ fer as $ mychrs)
Echo $ mychrs ."";
?>


Cnbruce cnrose cnjames cnanne
-- Reverse in the original order --
Cnanne cnjames cnrose cnbruce
Note that $ fer = array_reverse ($ fer) here; any sorting function before this is only declared reference, and the original array is not redefined, however, when I debug this function, I need to redefine it. This is because array_reverse () is the modified copy of the original array. if you do not need the original array, you can redefine it to overwrite the original array. Otherwise, you can define an array to save the copy, for example: $ fer_bak = array_reverse ($ fer );

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.