ONE NET WIKI Free and professional IT Wiki

Sorting arrays of PHP

2018-11-01 ONE NET WIKI

1.png

PHP has several functions that deal with sorting arrays.

The main differences are:

1) Some sort based on the array keys, whereas others by the values: $array['key'] = 'value';

2) Whether or not the correlation between the keys and values are maintained after the sort;

3) The order of the sort: alphabetical, low to high (ascending), high to low (descending), numerical, natural, random, or user defined

Note:

All of these sort functions act directly on the array variable itself, rather than return a new sorted array.


sort()

--- Sorts by: value

--- Maintains key association: no

--- Order of sort: low to high

--- Related functions: rsort()


rsort()

--- Sorts by: value

--- Maintains key association: no

--- Order of sort: high to low

--- Related functions: sort()


asort()

--- Sorts by: value

--- Maintains key association: yes

--- Order of sort: low to high

--- Related functions: arsort()


arsort()

--- Sorts by: value

--- Maintains key association: yes

--- Order of sort: high to low

--- Related functions: asort()


ksort()

--- Sorts by: key

--- Maintains key association: yes

--- Order of sort: low to high

--- Related functions: krsort()


krsort()

--- Sorts by: key

--- Maintains key association: yes

--- Order of sort: high to low

--- Related functions: ksort()


usort()

--- Sorts by: value

--- Maintains key association: no

--- Order of sort: user defined

--- Related functions: uasort()


uasort()

--- Sorts by: value

--- Maintains key association: yes

--- Order of sort: user defined

--- Related functions: uksort()


uksort()

--- Sorts by: key

--- Maintains key association: yes

--- Order of sort: user defined

--- Related functions: uasort()


natsort()

--- Sorts by: value

--- Maintains key association: yes

--- Order of sort: natural

--- Related functions: natcasesort()


natcasesort()

--- Sorts by: value

--- Maintains key association: yes

--- Order of sort: natural, case insensitive

--- Related functions: natsort()


shuffle()

--- Sorts by: value

--- Maintains key association: no

--- Order of sort: random

--- Related functions: array_rand()

2.jpg

Editor's Note: All the articles in this website are original. If reproduced, please indicate the source and link.
608
0
All Comments (0)
Expand to publish a comment

Web Development


Web Management


Web Marketing


Web Security