Home >Backend Development >PHP Tutorial >Introduction to 13 array sorting functions in PHP
The content shared with you in this article is about the PHP array sorting function. It has certain reference value. Friends in need can refer to it.
Among the functions, if there are u
, you can customize the comparison function; if there are k
, you can sort by key; if there are r
(reverse), in reverse order; if there is a(association)
, it must be a key-value association, except rsort()
usort()
sort()
shuffle()
, the others without a
are key-value associations, array_multisort()
key-value associations are maintained, numeric type Not maintained.
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, that is, the result of each sorting of elements with the same value is uncertain (associative array)). php7asort
arsort
uasort
has achieved stable sorting, php5 stable sorting: http://php.net/manual/zh/func...
Be careful when sorting arrays containing mixed type values, as sort() may produce unpredictable results.
can be used to sort multiple arrays at one time, or to sort multi-dimensional arrays according to a certain dimension or multiple dimensions. .
bool array_multisort ( array &$array1 [, mixed $array1_sort_order = SORT_ASC [, mixed $array1_sort_flags = SORT_REGULAR [, mixed $... ]]] )
Sort type flag:
SORT_REGULAR (default) - compare items in the usual way (do not modify type, distinguish size Write, uppercase letters will be sorted before lowercase letters)
SORT_NUMERIC - compare according to numerical size
SORT_STRING - compare according to string (size sensitive Write)
SORT_LOCALE_STRING - Compares strings based on the current localization settings. It uses locale information, which can be modified via setlocale().
SORT_NATURAL - "Natural sorting" of strings, similar to natsort()
SORT_FLAG_CASE - Can be combined (bitwise OR) SORT_STRING Or SORT_NATURAL to sort strings in a case-insensitive manner.
1.1 If there are multiple array parameters, $array2
will be based on the result of $array1
Sorting, like MySQL's group by
1.2 Case-insensitive sorting:
$array = array('Alpha', 'atomic', 'Beta', 'bank'); $array_lowercase = array_map('strtolower', $array); // 先复制一个转为小写数组 array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array); // 先对小写数组排序,再排序原数组 print_r($array);
1.3 Sort the database results
function array_orderby() { $args = func_get_args(); $data = array_shift($args); foreach ($args as $n => $field) { if (is_string($field)) { $tmp = array(); foreach ($data as $key => $row) $tmp[$key] = $row[$field]; $args[$n] = $tmp; } } $args[] = &$data; call_user_func_array('array_multisort', $args); return array_pop($args); }
Delete the original key name and sort the array (sequence)
bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
sort($fruits, SORT_NATURAL | SORT_FLAG_CASE);
The sorting result is the same as natcasesort()
.
Reverse sort the array
bool rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Maintain key-value association. Mainly used for sorting associative arrays where the order of cells is important.
bool asort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Reverse order and maintain index relationship (association, reverse)
bool arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Sort by key name
bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Press key name
Reverse order
bool krsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Use user-defined comparison function To sort the value
in an array, if the array to be sorted needs to be sorted by an unusual criterion, this function should be used.
bool usort ( array &$array , callable $value_compare_func )
$value_compare_func( mixed $a, mixed $b )
is less than, equal to or greater than $a
##$b, the comparison function must return an integer less than, equal to, or greater than 0 accordingly.
usort($a, array("TestObj", "cmp_obj"))Object attribute sorting
bool uasort ( array &$array , callable $value_compare_func )
in the array
bool uksort ( array &$array , callable $key_compare_func )
bool natcasesort ( array &$array )
bool natsort ( array &$array )
bool shuffle ( array &$array )
Introduction to 10 commonly used string functions in PHP and how to use them
The above is the detailed content of Introduction to 13 array sorting functions in PHP. For more information, please follow other related articles on the PHP Chinese website!