Array_multisort(), asort(), arsort(), krsort(), ksort(), natcasesort(), natsort(), rsort(), shuffle(), sort(), etc. can be used in PHP Function sorts array elements.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
PHP has some functions for sorting arrays Functions:
The main differences are:
Some functions sort based on the keys of the array, while others sort based on the values: $array[' key'] = 'value';.
Whether the association between keys and values can be maintained after sorting, means that after sorting, the keys of the array may be reset to numeric types (0,1,2.. .).
The sorting order is: alphabetical order, from low to high (ascending order), from high to low (descending order), numerical sorting, natural sorting, random order or user-defined sorting .
Note: All the following sorting functions act directly on the array itself, rather than returning a new ordered array.
The following functions have undefined order for equal elements in the array after sorting. (That is, the order between equal elements is unstable).
Function (method) name | Sort by | Array index keys maintain | sorted order |
---|---|---|---|
##array_multisort() | ValueThe key value association is maintained, the numeric type is not maintained | The first array or specified by the option | |
asort( ) | The valueis | from low to high | |
arsort() | The valueis | from high to low | |
krsort() | Keyis | from high to low | |
ksort() | keyis | from low to high | |
natcasesort() | valueis | Natural sorting, case insensitive | |
natsort() | The valueis | Natural sort | |
rsort() | ValueNo | from high to low | |
shuffle() | ValueNo | Random | |
sort() | ValueNo | from low to high | |
uasort() | The valueis | defined by the user | |
uksort() | Keysare | defined by the user | |
Value | No | Defined by user |
The above is the detailed content of What is the method for sorting array elements in php. For more information, please follow other related articles on the PHP Chinese website!