PHP is still more commonly used, so I studied the PHP array sorting, here to take out and share with you, hope to be useful to everyone. After learning about the Usort custom sort, let's look at sort (), which is the originator of the sort function in the array, and you'll definitely find that all the PHP array sort functions have a sort suffix. Function prototypes: bool Sort (array &array [, int sort_flags]) Description: Basically each function will have an optional parameter, sort is no exception. This optional parameter indicates a habit.
There are several types of options available:
sort_regular– Normal comparison unit (does not change type)//Sort by ASCII value (b is greater than a)
The sort_numeric– unit is used as a number to compare//for integers and floating-point numbers.
The sort_string– unit is compared as a string.
sort_locale_string– the cell as a string compared to the current locale setting
Or look at an example from the Help manual:
- Php
- $ Fruits Array("Lemon", "orange", "banana", "apple");
- Sort ($fruits);
- foreach ($fruits as $key => $val) {
- echo "fruits[". $key. "] = " . $val. "N";
- }
- ?>
The result of this sort of PHP array is as follows:
- Fruits[0] = Apple
- FRUITS[1] = Banana
- FRUITS[2] = Lemon
As you can see, the lemon, which was indexed at 0, became Apple after sorting. To keep the original key/value constant, you can change the sort () to Asort (). In addition, the two functions are arranged in ascending order of the keys (a in front of B). If you want to make it descending, as long as you use Rsort () instead, the corresponding arsort () is to keep the original key/value associated.
Below is a brief introduction to the following Ksort () function
This function is primarily used for associative arrays, and its "sister" function Uksort () is compared using a custom function. Bottom line: Ksort () for association progression, sorts the keys and preserves the key name-to-data association. The natural ordering of arrays, so far as we know the sort function is either in the default way, or in a custom way to sort. Then Natsort () uses a humane sort method to sort the array. In this case, you can view the manual, if you do not have the PHP help manual, you can browse the site of the online Help manual to view
- Shuffle ()//use of functions
- Shuffle ()//used to randomly sort an array, this is a bit like a lottery, and the rest is not much to say. Look at it when it's time to use it.
- Array_rand ()//use of functions
- Array_rand ()//function is similar to shuffle () and is also a random return element in an array, such as a downgrade:
To this I think the sort of array we have some understanding, if there is any "regret" later to make up for it.
http://www.bkjia.com/PHPjc/446529.html www.bkjia.com true http://www.bkjia.com/PHPjc/446529.html techarticle PHP is still more commonly used, so I studied the PHP array sorting, here to take out and share with you, hope to be useful to everyone. After understanding the Usort custom sort, we then ...