Home  >  Article  >  Backend Development  >  What is the method for sorting array elements in php

What is the method for sorting array elements in php

青灯夜游
青灯夜游Original
2021-05-20 17:43:232993browse

Array_multisort(), asort(), arsort(), krsort(), ksort(), natcasesort(), natsort(), rsort(), shuffle(), sort(), etc. can be used in PHP Function sorts array elements.

What is the method for sorting array elements in php

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

ValueThe key value association is maintained, the numeric type is not maintainedThe first array or specified by the optionThe value is from low to highThe value is from high to low Key is from high to low keyisfrom low to highvalueisNatural sorting, case insensitiveThe value is Natural sortValueNofrom high to lowValueNoRandomValueNofrom low to highThe value is defined by the userKeys are defined by the user##usort()Recommended learning: "
Sort function attributes
Function (method) name Sort by Array index keys maintain sorted order
##array_multisort()
asort( )
arsort()
krsort()
ksort()
natcasesort()
natsort()
rsort()
shuffle()
sort()
uasort()
uksort()
Value No Defined by user
PHP Video Tutorial

"

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn