usort

(PHP 4, PHP 5, PHP 7)

usort 使锟斤拷锟矫伙拷锟皆讹拷锟斤拷谋冉虾锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷械锟街碉拷锟斤拷锟斤拷锟斤拷锟�

说锟斤拷

usort ( array &$array , callable $value_compare_func ) : bool

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟矫伙拷锟皆讹拷锟斤拷谋冉虾锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷械锟街碉拷锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟揭拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷要锟斤拷一锟街诧拷寻锟斤拷锟侥憋拷准锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷么应锟斤拷使锟矫此猴拷锟斤拷锟斤拷

Note:

If two members compare as equal, their relative order in the sorted array is undefined.

Note: 锟剿猴拷锟斤拷为 array 锟叫碉拷元锟截革拷锟斤拷锟铰的硷拷锟斤拷锟斤拷锟解将删锟斤拷原锟叫的硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟角斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

锟斤拷锟斤拷

array

锟斤拷锟斤拷锟斤拷锟斤拷锟�

cmp_function

锟节碉拷一锟斤拷锟斤拷锟斤拷小锟节o拷锟斤拷锟节伙拷锟斤拷诘诙锟斤拷锟斤拷锟斤拷锟绞憋拷锟斤拷帽冉虾锟斤拷锟斤拷锟斤拷锟斤拷锟接︼拷胤锟斤拷锟揭伙拷锟叫★拷冢锟斤拷锟斤拷诨锟斤拷锟斤拷 0 锟斤拷锟斤拷锟斤拷锟斤拷

callback ( mixed $a, mixed $b ) : int
Caution

Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

锟斤拷锟斤拷值

锟缴癸拷时锟斤拷锟斤拷 TRUE锟斤拷 锟斤拷锟斤拷锟斤拷失锟斤拷时锟斤拷锟斤拷 FALSE锟斤拷

锟斤拷锟斤拷

Example #1 usort() 锟斤拷锟斤拷

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

$a = array(32561);

usort($a"cmp");

foreach (
$a as $key => $value) {
    echo 
"$key$value\n";
}
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

0: 1
1: 2
2: 3
3: 5
4: 6

Note:

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫★拷锟斤拷锟斤拷锟斤拷锟� sort() 锟斤拷锟斤拷锟斤拷锟斤拷锟绞★拷

Example #2 使锟矫讹拷维锟斤拷锟斤拷锟� usort() 锟斤拷锟斤拷

<?php
function cmp($a$b)
{
    return 
strcmp($a["fruit"], $b["fruit"]);
}

$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";

usort($fruits"cmp");

while (list(
$key$value) = each($fruits)) {
    echo 
"$fruits[$key]: " $value["fruit"] . "\n";
}
?>

锟斤拷锟斤拷锟斤拷锟轿拷锟斤拷锟绞憋拷锟�$a 锟斤拷 $b 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷谩锟�

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

$fruits[0]: apples
$fruits[1]: grapes
$fruits[2]: lemons

Example #3 usort() example using a member function of an object

<?php
class TestObj {
    var 
$name;

    function 
TestObj($name)
    {
        
$this->name $name;
    }

    
/* This is the static comparing function: */
    
static function cmp_obj($a$b)
    {
        
$al strtolower($a->name);
        
$bl strtolower($b->name);
        if (
$al == $bl) {
            return 
0;
        }
        return (
$al $bl) ? +: -1;
    }
}

$a[] = new TestObj("c");
$a[] = new TestObj("b");
$a[] = new TestObj("d");

usort($a, array("TestObj""cmp_obj"));

foreach (
$a as $item) {
    echo 
$item->name "\n";
}
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

b
c
d

Example #4 usort() example using a closure to sort a multi-dimensional array

<?php
$array
[0] = array('key_a' => 'z''key_b' => 'c');
$array[1] = array('key_a' => 'x''key_b' => 'b');
$array[2] = array('key_a' => 'y''key_b' => 'a');

function 
build_sorter($key) {
    return function (
$a$b) use ($key) {
        return 
strnatcmp($a[$key], $b[$key]);
    };
}

usort($arraybuild_sorter('key_b'));

foreach (
$array as $item) {
    echo 
$item['key_a'] . ', ' $item['key_b'] . "\n";
}
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

y, a
x, b
z, c

锟轿硷拷