PHP array element ascending, descending, and re-sorting functions

Source: Internet
Author: User
Tags shuffle

1. quickly create an array function range ()

for example, range () function allows you to quickly create a number array from 1 to 9:


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


of course, a number array ranging from 9 to 1 is created using range (9, 1. In addition, range () can also create a character array from A to Z:


$ numbers = range (A, Z);
foreach ($ numbers as $ mychrs)
echo $ mychrs. "";
?>


use case-sensitive character arrays, such as range (A, Z) and range (A, Z) is different.

range () the function also has the third parameter , this parameter is used to set the step size , such as range, 3 ) The created array element is: 1, 4, 7

2, sort 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. The function is sort () . For example:


$ people = array ('name', 'sex', 'nation', 'birth ');
foreach ($ people as $ mychrs)
echo $ mychrs. "";
sort ($ people);
echo"
--- sort ---
";
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:


<? 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 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, except for the ascending function, descending or inverse sort functions , rsort () function , for example:


$ num1 = range (1, 9);
rsort ($ num1);


here it is actually equivalent to range (9, 1)

3, sort joined arrays in PHP

In addition to numeric index arrays, PHP also supports related arrays. For example, the following array is a related (associated) array :


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


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


reverse sorting of the sort () ascending function with regular arrays rsort () corresponding to the descending function, the associated array also has the corresponding descending function: asort () function and arsort () function, ksort () function and krsort () function.

memory: The prototype function is sort (). A and K indicate that the associated array must be pre-configured, and R is used for reverse sorting.

4, random sorting of PHP array elements

use shuffle () in PHP () function sorts array elements randomly, for example,


$ 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

array_reverse () function sorts array elements in reverse order. For example:


$ fer = array ('cnbruce ', 'cnrose', 'cnjames ', 'cnanne ');
foreach ($ FER as $ mychrs)
echo $ mychrs. "";

$ fer = array_reverse ($ FER);
echo"
-- reverse in the original order --
";
forea CH ($ FER as $ mychrs)
echo $ mychrs. "";
?>


cnbruce cnrose cnjames cnanne
-- reverse in the original order --
cnanne cnjames cnrose cnbruce

note the $ fer = array_reverse ($ FER); Any sorting function prior to this is just a declaration reference, the original array is not redefined, but it needs to be redefined when I debug the function. 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.