Introduction to 13 array sorting functions in PHP

Source: Internet
Author: User
Tags array sort shuffle
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

    • Can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional.

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);
    • 1.3 Sorting the results of a database

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

    • Delete the original key name array sort (order)

BOOL Sort (array & $array [, int $sort _flags = sort_regular])
    • sort($fruits, SORT_NATURAL | SORT_FLAG_CASE);Sort the natcasesort() same result.

3.rsort

    • Reverse sequence of an array

BOOL Rsort (array & $array [, int $sort _flags = sort_regular])

4.asort

    • Keep the key Value Association (association). It is primarily used to sort the associative arrays that are important for the cell order.

BOOL Asort (array & $array [, int $sort _flags = sort_regular])

5.arsort

    • Reverse and maintain index relationships (association,reverse)

BOOL Arsort (array & $array [, int $sort _flags = sort_regular])

6.ksort

    • 键名Sort By

BOOL Ksort (array & $array [, int $sort _flags = sort_regular])

7.krsort

    • In 键名 reverse order

BOOL Krsort (array & $array [, int $sort _flags = sort_regular])

8.usort

    • Using a user-defined comparison function to sort the arrays, the array to be sorted needs to be sorted with an unusual standard, and this function should be used.

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

    • Use a user-defined comparison function to sort the values in the array and keep the index associated

BOOL Uasort (array & $array, callable $value _compare_func)

10.uksort

    • Sorting in an array using a user-defined comparison function 键名

BOOL Uksort (array & $array, callable $key _compare_func)

11.natcasesort

    • Natural sorting of uppercase and lowercase letters (number of letters and rows), keeping key values associated

BOOL Natcasesort (array & $array)

12.natsort

    • Sort the array with the "natural sort" algorithm to keep the key values associated

BOOL Natsort (array & $array)

13.shuffle

    • This function disrupts (the order of randomly arranged cells) an array. Key value is not associated

BOOL Shuffle (array & $array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.