Skip to main content

Module sort

Module sort 

Source
Expand description

PHP’s array sorting and set functions: sort, ksort, array_unique, array_diff, array_intersect, in_array and array_search, with the SORT_* flags.

PHPRust
sort($a, $flags)sort
ksort($a, $flags)ksort
array_unique($a, $flags)array_unique
array_diff($a, ...$b), array_intersectarray_diff, array_intersect
in_array($n, $h, $strict), array_searchin_array, array_search
SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_FLAG_CASE, SORT_NATURAL, SORT_LOCALE_STRINGSortFlags

PHP 8 sorts are stable: zend_sort (insertion sort up to 16 elements, then a quicksort) with the original position breaking ties. PHP’s comparisons are not always transitive (null == 0, null == "", but 0 > ""), so the result depends on the algorithm: zend_sort is a port of it, comparison for comparison. SORT_LOCALE_STRING compares like strcoll in the C locale (byte order of the C strings, so up to the first NUL byte), which is what the engine uses without setlocale. Functions that turn values into strings fail like PHP when they meet an object (Error), so the value sorts and set functions return Result (the skeleton in rfc/rust-database.md has ()).

Enums§

SortFlags
The $flags of the sort functions.

Functions§

array_diff
array_diff($a, ...$others): the entries of a whose string value is in none of others, keys kept.
array_intersect
array_intersect($a, ...$others): the entries of a whose string value is in every one of others, keys kept (php_array_intersect: sorted copies walked together).
array_search
array_search($needle, $haystack, $strict): the key of the first match.
array_unique
array_unique($a, $flags) (PHP’s default flag is SORT_STRING): the first of each group of equal values, keys kept.
in_array
in_array($needle, $haystack, $strict).
ksort
ksort($a, $flags).
sort
sort($a, $flags): values in order, keys renumbered.
strcasecmp
zend_binary_strcasecmp_l: ASCII case-insensitive.
strcoll
strcoll in the C locale: byte order of the C strings (up to the first NUL).
strnatcmp
strnatcmp_ex: natural order (strnatcmp, or strnatcasecmp with fold_case).
zend_sort
zend_sort: the engine’s hybrid insertion sort / quicksort. cmp returns a C comparison result; only > 0 matters, as in the engine.