usort
Description
Syntax
usort( array &$array, callable $callback ): true
Parameters
array
The input array.
callback
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.
callback( mixed $a, mixed $b ): int
a
first argument
b
second argument
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.
Return
Always returns true.
Examples
1
<? function myfunction($a, $b) { if($a < $b) { return -1; } else if($a > $b) { return 1; } else { return 0; } } $array = array(3, 2, 5, 6, 1); $callback = "myfunction"; usort($array, $callback); print_r($array); ?>
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 5 [4] => 6 )
2
<? function myfunction($a, $b) { return strcmp($a, $b); } $array[0] = "lemons"; $array[1] = "apples"; $array[2] = "grapes"; $callback = "myfunction"; usort($array, $callback); foreach($array as $key => $value) { echo "\$array[$key]: $value\n"; } ?>
$array[0]: apples $array[1]: grapes $array[2]: lemons
3
<? class myclass { var $name; function __construct($name) { $this->name = $name; } static function myfunction($a, $b) { $al = strtolower($a->name); $bl = strtolower($b->name); if($al == $bl) { return 0; } return ($al > $bl) ? 1 : -1; } } $array[] = new myclass("c"); $array[] = new myclass("b"); $array[] = new myclass("d"); $callback = array("myclass", "myfunction"); usort($array, $callback); foreach($array as $item) { echo $item->name . "\n"; } ?>
b c d
4
<? function myfunction($key) { return function($a, $b) use ($key) { return strnatcmp($a[$key], $b[$key]); }; } $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'); $callback = myfunction('key_b'); usort($array, $callback); foreach($array as $item) { echo $item['key_a'] . ', ' . $item['key_b'] . "\n"; } ?>
y, a x, b z, c
Links
Array
- array
- array_change_key_case
- array_chunk
- array_column
- array_combine
- array_count_values
- array_diff
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_fill
- array_fill_keys
- array_filter
- array_flip
- array_intersect
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_key_exists
- array_key_first
- array_key_last
- array_keys
- array_map
- array_merge
- array_merge_recursive
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace
- array_replace_recursive
- array_reverse
- array_search
- array_shift
- array_slice
- array_splice
- array_sum
- array_udiff
- array_udiff_assoc
- array_udiff_uassoc
- array_uintersect
- array_uintersect_assoc
- array_uintersect_uassoc
- array_unique
- array_unshift
- array_values
- array_walk
- array_walk_recursive
- arsort
- asort
- compact
- count
- current
- end
- extract
- in_array
- key
- key_exists
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort