Rust

sort

Module

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

Every function here is checked against the engine by bin/compat fuzz php-std (operations sort.*).

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 ()).

Contents

use php_std::sort::SortFlags;
Rust
pub enum SortFlags

The $flags of the sort functions.

use php_std::sort::array_diff;
Rust
pub fn array_diff<X: Extension>(a: &Array<X>, others: &[&Array<X>]) -> Result<Array<X>, EngineError>

array_diff($a, ...$others): the entries of a whose string value is in none of others, keys kept.

use php_std::sort::array_intersect;
Rust
pub fn array_intersect<X: Extension>(a: &Array<X>, others: &[&Array<X>]) -> Result<Array<X>, EngineError>

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).

use php_std::sort::array_unique;
Rust
pub fn array_unique<X: Extension>(a: &Array<X>, flags: SortFlags) -> Result<Array<X>, EngineError>

array_unique($a, $flags) (PHP's default flag is SORT_STRING): the first of each group of equal values, keys kept.

use php_std::sort::in_array;
Rust
pub fn in_array<X: Extension>(needle: &Value<X>, haystack: &Array<X>, strict: bool) -> bool

in_array($needle, $haystack, $strict).

use php_std::sort::ksort;
Rust
pub fn ksort<X: Extension>(a: &mut Array<X>, flags: SortFlags)

ksort($a, $flags).

use php_std::sort::sort;
Rust
pub fn sort<X: Extension>(a: &mut Array<X>, flags: SortFlags) -> Result<(), EngineError>

sort($a, $flags): values in order, keys renumbered.

use php_std::sort::strcasecmp;
Rust
pub fn strcasecmp(a: &[u8], b: &[u8]) -> i32

zend_binary_strcasecmp_l: ASCII case-insensitive.

use php_std::sort::strcoll;
Rust
pub fn strcoll(a: &[u8], b: &[u8]) -> i32

strcoll in the C locale: byte order of the C strings (up to the first NUL).

use php_std::sort::strnatcmp;
Rust
pub fn strnatcmp(a: &[u8], b: &[u8], fold_case: bool) -> i32

strnatcmp_ex: natural order (strnatcmp, or strnatcasecmp with fold_case).

use php_std::sort::zend_sort;
Rust
pub fn zend_sort<T>(v: &mut [T], cmp: &mut impl FnMut(&T, &T) -> i32)

zend_sort: the engine's hybrid insertion sort / quicksort. cmp returns a C comparison result; only > 0 matters, as in the engine.