ArrayObject::uasort

(PHP 5 >= 5.2.0, PHP 7)

ArrayObject::uasortSort the entries with a user-defined comparison function and maintain key association

说明

public ArrayObject::uasort ( callable $cmp_function ) : void

This function sorts the entries such that keys maintain their correlation with the entry that they are associated with, using a user-defined comparison function.

This is used mainly when sorting associative arrays where the actual element order is significant.

参数

cmp_function

Function cmp_function should accept two parameters which will be filled by pairs of entries. The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

返回值

没有返回值。

范例

Example #1 ArrayObject::uasort() example

<?php
// Comparison function
function cmp($a$b) {
    if (
$a == $b) {
        return 
0;
    }
    return (
$a $b) ? -1;
}

// Array to be sorted
$array = array('a' => 4'b' => 8'c' => -1'd' => -9'e' => 2'f' => 5'g' => 3'h' => -4);
$arrayObject = new ArrayObject($array);
print_r($arrayObject);

// Sort and print the resulting array
$arrayObject->uasort('cmp');
print_r($arrayObject);
?>

以上例程会输出:

Array
(
    [a] => 4
    [b] => 8
    [c] => -1
    [d] => -9
    [e] => 2
    [f] => 5
    [g] => 3
    [h] => -4
)
Array
(
    [d] => -9
    [h] => -4
    [c] => -1
    [e] => 2
    [g] => 3
    [a] => 4
    [f] => 5
    [b] => 8
)

参见

相关文章
php arrayiterator sort with a user defined comparison function and maintain index associationphp arrayiterator sort by keys using a user defined comparison functionphp arrayobject sort the entries by valuephp arrayobject sort the entries by keyphp arrayobject sort entries using a natural order algorithmphp arrayobject sort the entries with a user defined comparison function and maintain key associationphp arrayobject sort the entries by keys using a user defined comparison functionphp collator sort array maintaining index associationphp ibm db2 函数 gets a user defined size of lob files with each invocationphp fdf 函数 call a user defined function for each document valuephp ncurses 函数 check for presence of a function key on terminal keyboardphp ncurses 函数 turn off the given attributes for soft function key labelsphp newt 函数 set a callback function which gets invoked when user presses the suspend keyphp oci8 函数 register a user defined callback function for oracle database tafphp oci8 函数 unregister a user defined callback function for oracle database tafphp sodium 函数 compute a shared secret given a user s secret key and another user s public keyphp sqlite 函数 registers a regular user defined function for use in sql statementsphp sqlite pdo registers an aggregating user defined function for use in sql statementsphp sqlite pdo registers a user defined function for use as a collating function in sql statementsphp sqlite pdo registers a user defined function for use in sql statements
关注编程学问公众号