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

Source: Internet
Author: User
By default, the application sort ($ sort les) is sorted in ascending order based on the defined values of the elements. the expression of the asort () function can be applied to the associated array, the most important thing in the joined array is to sort the key words (such as xm, xb, and mz) in ascending order. This method uses the ksort () function.

The excerpt from the PHP array learning section shows how to create the most basic PHP array and how to display array elements. You need to learn deeply about the Coherent control of the PHP array. The first contact is the ascending and descending order headers of array elements.

1. quickly create an array function range ()

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


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


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


Use case sensitivity when applying 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 of 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:


$ People = array ('name', '***', '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 ***. 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 to demonstrate whether the ascending order is used to compare numbers or strings. For example:


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. The numbers are listed in ascending order: 3, 26, and the characters are listed in ascending order: 26, 3.

In PHP, in addition to the ascending function, there are also descending or reverse sort functions, which are rsort () functions, such:


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


This is actually equivalent to range (9, 1)

3. sorting of associated arrays in PHP

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


$ Les = array ('xm '=> 'name', 'xb' => '***', 'mz' => 'nation ', 'CS '=> 'birth ');


By default, the application sort ($ sort les) is sorted in ascending order based on the defined values of the elements. the expression of the asort () function can be applied to the associated array, the most important thing in the joined array is to sort the key words (such as xm, xb, and mz) in ascending order. This method uses the ksort () function.

 

 


$ Les = array ('xm '=> 'name', 'xb' => '***', '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 key words 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 the conventional array, and the associated array also has the corresponding descending function: asort () functions: arsort (), ksort (), and krsort.

Memory: The prototype function is sort (). among them, 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

The shuffle () function is used in PHP to randomly sort array elements, 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

The array_reverse () function can be used in PHP to sort 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 --
';
Foreach ($ fer as $ mychrs)
Echo $ mychrs .'';
?>


Cnbruce cnrose cnjames cnanne
-- Reverse in the original order --
Cnanne cnjames cnrose cnbruce

Pay attention to the $ fer = array_reverse ($ fer) here. any sorting function prior to this is just a declaration 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 corrected copy of the original array. if you do not need the original array, you can redefine the original array to reach the enveloped target. Otherwise, you can define an array to retain 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.