This article to share the content is about the PHP array sorting function, has a certain reference value, the need for friends can refer to.
function, you u
can customize the comparison function; Yes k
, sort by key; there is r
(reverse), reverse; Yes a(association)
, it must be the key value Association, except rsort()
usort()
sort()
shuffle()
, the other a
is the key value Association , array_multisort()
key-value associations are persisted, and numeric types are not persisted.
All of the following sorting functions are directly acting on the array itself, rather than returning a new ordered array.
The following functions are not defined in the order in which they are sorted, for elements that are equal in the array. (That is, the order of the equal elements is unstable, that is, the result of each ordering of elements with the same value is indeterminate (associative arrays)). PHP7 asort
arsort
uasort
has achieved stable sequencing, php5 stable sort: http://php.net/manual/zh/func ...
Be careful when sorting arrays that contain mixed-type values, because sort () can produce unpredictable results.
1.array_multisort
BOOL Array_multisort (Array & $array 1 [, mixed $array 1_sort_order = Sort_asc [, mixed $array 1_sort_flags = Sort_regula R [, Mixed $ ...]] )
Sort Type flag:
Sort_regular (default)-Compare items by usual method (do not modify type, case-sensitive, uppercase letters precede lowercase)
Sort_numeric-Compare by number size
Sort_string-Comparison by string (case-sensitive)
Sort_locale_string-based on the current localization settings, by string comparison. It uses locale information and can modify this information through setlocale ().
Sort_natural-"natural sort" with strings, similar to Natsort ()
Sort_flag_case-You can combine (bitwise OR OR) sort_string or sort_natural case-insensitive to sort strings.
1.1 If there are multiple array parameters, $array2
$array1
The results are sorted according to the result, like MySQL'sgroup by
1.2 Case-insensitive sorting:
$array = Array (' Alpha ', ' Atomic ', ' Beta ', ' Bank '), $array _lowercase = Array_map (' Strtolower ', $array); First copy a converted lowercase array array_multisort ($array _lowercase, SORT_ASC, sort_string, $array); Sort the lowercase array first, and then sort the original array print_r ($array);
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);}
2.sort
BOOL Sort (array & $array [, int $sort _flags = sort_regular])
3.rsort
BOOL Rsort (array & $array [, int $sort _flags = sort_regular])
4.asort
BOOL Asort (array & $array [, int $sort _flags = sort_regular])
5.arsort
BOOL Arsort (array & $array [, int $sort _flags = sort_regular])
6.ksort
BOOL Ksort (array & $array [, int $sort _flags = sort_regular])
7.krsort
BOOL Krsort (array & $array [, int $sort _flags = sort_regular])
8.usort
BOOL Usort (array & $array, callable $value _compare_func)
$value_compare_func( mixed $a, mixed $b )
At $a
less than, equal to, or greater than, $b
the comparison function must return an integer less than, equal to, or greater than 0.
usort($a, array("TestObj", "cmp_obj"))
object property Ordering
9.uasort
BOOL Uasort (array & $array, callable $value _compare_func)
10.uksort
BOOL Uksort (array & $array, callable $key _compare_func)
11.natcasesort
BOOL Natcasesort (array & $array)
12.natsort
BOOL Natsort (array & $array)
13.shuffle
BOOL Shuffle (array & $array)