This article discusses how to sort PHP arrays. After learning about usort custom sorting, let's take a look at sort (). This function is the originator of the sorting function in the array. you will surely find that all PHP array sorting functions willAfter learning about usort custom sorting, let's take a look at sort (). This function is the originator of the sorting function in the array, you will surely find that all PHP array sorting functions are suffixed with sort. Function prototype: bool sort (array & array [, int sort_flags]) description: Basically, each function has an optional parameter, and sort is no exception. This optional parameter specifies a habit.
The following types are available:
◆ SORT_REGULAR-normal comparison unit (without changing the type) // Sort by ASCII value (B is greater than)
◆ SORT_NUMERIC-the unit is used as a number to compare // this parameter is commonly used for integers and floating-point numbers.
◆ SORT_STRING-the unit is compared as a string
◆ SORT_LOCALE_STRING-compare the unit as a string based on the current region (locale) settings
Let's take a look at the 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 sorting the PHP array is as follows:
- fruits[0] = apple
- fruits[1] = banana
- fruits[2] = lemon
- fruits[3] = orange
You can find that the lemon with an index of 0 is then sorted and then changed to apple. To keep the original key/value unchanged, you can replace sort () with asort. These two functions sort keys in ascending order (a is prior to B ). To make it in descending order, you only need to use rsort () instead. the corresponding arsort () maintains the relevance of the original key/value.
The following describes the ksort () function.
This function is mainly used to exist associated arrays. its "sister" function uksort () is compared using a custom function. In a word, ksort () sorts keys for the correlation level and retains the association from key name to data. The natural sorting of arrays. the sorting functions we know so far are either sorted by default or by custom means. Natsort () uses a user-friendly sorting method to sort arrays. If you have no PHP help manual on hand, you can view the online help manual on this site.
- Shuffle () // function usage
- Shuffle () // is used to sort an array randomly. this is a bit like drawing lots, so we will not talk about anything else. Let's take a look.
- Array_rand () // function usage
- The array_rand () // function is similar to the shuffle () function. it is also an element in the random return array. the following call is performed:
- Array_rand (array [, int num_req]) // optional parameter indicates the number of returned results.
This article introduces the PHP array sorting method and hopes to help you.
After learning about usort custom sorting, sort will take a look at sort (). This function is the originator of the sort function in the array, you will surely find that all PHP array sorting functions will...