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.
| PHP | Rust |
|---|---|
sort($a, $flags) | sort |
ksort($a, $flags) | ksort |
array_unique($a, $flags) | array_unique |
array_diff($a, ...$b), array_intersect | array_diff, array_intersect |
in_array($n, $h, $strict), array_search | in_array, array_search |
SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_FLAG_CASE, SORT_NATURAL, SORT_LOCALE_STRING | SortFlags |
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§
- Sort
Flags - The
$flagsof the sort functions.
Functions§
- array_
diff array_diff($a, ...$others): the entries ofawhose string value is in none ofothers, keys kept.- array_
intersect array_intersect($a, ...$others): the entries ofawhose string value is in every one ofothers, 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 isSORT_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
strcollin the C locale: byte order of the C strings (up to the first NUL).- strnatcmp
strnatcmp_ex: natural order (strnatcmp, orstrnatcasecmpwithfold_case).- zend_
sort zend_sort: the engine’s hybrid insertion sort / quicksort.cmpreturns a C comparison result; only> 0matters, as in the engine.