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

Source: Internet
Author: User

1. quickly create an array function range ()
For example, the range () function can quickly create a number array from 1 to 9:

Copy codeThe Code is as follows: <? Php
$ 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:Copy codeThe Code is as follows: <? Php
$ 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:

Copy codeThe Code is as follows: <? Php
$ People = array ('name', 'sex', 'nation', 'birth ');
Foreach ($ people as $ mychrs)
Echo $ mychrs ."";
Sort ($ people );
Echo "<br/> --- sorted --- <br/> ";
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:Copy codeThe Code is as follows: <? Php
Echo "--- sort by numbers in ascending order --- <br/> ";
$ Num2 = array ('26', '3 ',);
Sort ($ num2, SORT_NUMERIC );
Foreach ($ num2 as $ mychrs)
Echo $ mychrs ."";
Echo "<br/> --- sort by character in ascending order --- <br/> ";
$ 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:Copy codeThe 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:

Copy codeThe Code is as follows: $ using LES = 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.Copy codeThe Code is as follows: <? Php
$ Doneles = array ('xm '=> 'name', 'xb' => 'sex', 'mz' => 'nation ', 'cs '=> 'birth ');
Foreach ($ minutes les as $ mychrs)
Echo $ mychrs ."";
Echo "<br/> -- sort by element values in ascending order -- <br/> ";
Asort ($ minutes les );
Foreach ($ minutes les as $ mychrs)
Echo $ mychrs ."";
Echo "<br/> -- sort by keywords in ascending order -- <br/> ";
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:

Copy codeThe Code is as follows: <? Php
$ 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:

Copy codeThe Code is as follows: <? Php
$ Fer = array ('cnbruce ', 'cnrose', 'cnjames ', 'cnanne ');
Foreach ($ fer as $ mychrs)
Echo $ mychrs ."";
$ Fer = array_reverse ($ fer );
Echo "<br/> -- reverse in the original order -- <br/> ";
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.