Introduction to 13 array sorting functions in PHP

不言
Release: 2023-04-03 10:50:02
Original
1909 people have browsed it

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.

1.array_multisort

  • 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 $... ]]] )
Copy after login

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);
Copy after login
  • 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);
}
Copy after login

2.sort

  • Delete the original key name and sort the array (sequence)

bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Copy after login
  • sort($fruits, SORT_NATURAL | SORT_FLAG_CASE);The sorting result is the same as natcasesort().

3.rsort

  • Reverse sort the array

bool rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Copy after login

4.asort

  • 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 ] )
Copy after login

5.arsort

  • Reverse order and maintain index relationship (association, reverse)

bool arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Copy after login

6.ksort

  • Sort by key name

bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Copy after login

7.krsort

  • Press key nameReverse order

bool krsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Copy after login

8.usort

  • 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 )
Copy after login
  • $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

  • ##9 .uasort

    Use user-defined comparison function to sort values ​​in an array and maintain index association
  • bool uasort ( array &$array , callable $value_compare_func )
    Copy after login
  • 10.uksort

    Use user-defined comparison function to sort the
  • key names

    in the array

    bool uksort ( array &$array , callable $key_compare_func )
    Copy after login
  • 11.natcasesort

    Natural sorting that does not distinguish between uppercase and lowercase letters (numbers after letters), maintaining key-value association
  • bool natcasesort ( array &$array )
    Copy after login
  • 12.natsort

    Use the "natural sorting" algorithm to sort the array and keep the key-value association
  • bool natsort ( array &$array )
    Copy after login
  • 13.shuffle

    This function scrambles (randomly arranges the order of cells) an array. The key value is not associated
  • bool shuffle ( array &$array )
    Copy after login
  • Related recommendations:

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template