uasort
(PHP 4, PHP 5)
uasort — Sort an array with a user-defined comparison function and maintain index association
Description
This function sorts an array such that array indices maintain their correlation with the array elements 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.
Note:
If two members compare as equal, their relative order in the sorted array is undefined.
Parameters
Return Values
Returns TRUE
on success or FALSE
on failure.
Examples
Example #1 Basic uasort() example
<?php
// Comparison function
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
print_r($array);
// Sort and print the resulting array
uasort($array, 'cmp');
print_r($array);
?>
The above example will output:
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 )
See Also
- usort() - Sort an array by values using a user-defined comparison function
- The comparison of array sorting functions
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения, относящиеся к переменным и типам
- Массивы
- array_change_key_case
- array_chunk
- array_column
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_exists
- array_keys
- array_map
- array_merge_recursive
- array_merge
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace_recursive
- array_replace
- array_reverse
- array_search
- array_shift
- array_slice
- array_splice
- array_sum
- array_udiff_assoc
- array_udiff_uassoc
- array_udiff
- array_uintersect_assoc
- array_uintersect_uassoc
- array_uintersect
- array_unique
- array_unshift
- array_values
- array_walk_recursive
- array_walk
- array
- arsort
- asort
- compact
- count
- current
- each
- end
- extract
- in_array
- key_exists
- key
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort
Коментарии
Is it just me, or are the examples below misleading, and actually demonstrating situations that would be more appropriate for usort()?
After trying to make sense of the uasort() description, it sounds like it's more for sorting a 1D array like this:
"john" => "$23.12"
"tim" => "$6.50"
"bob" => "$18.54"
and getting back:
"tim" => "$6.50"
"bob" => "$18.54"
"john" => $23.12"
(assuming, of course, that your sort function is lopping off the $ and evaluating as a number -- which would complicate the use of asort() ;)
a quick reminder on the syntax if you want to use uasort in a Class or Object:
<?php
// procedural:
uasort($collection, 'my_sort_function');
// Object Oriented
uasort($collection, array($this, 'mySortMethod'));
// Objet Oriented with static method
uasort($collection, array('self', 'myStaticSortMethod'));
?>
An Example using anonymous function.
Anonymous functions make some time the code easier to understand.
<?php
$fruits = array('Orange9','Orange11','Orange10','Orange6','Orange15');
uasort ( $fruits , function ($a, $b) {
return strnatcmp($a,$b); // or other function/code
}
);
print_r($fruits);
?>
returns
Array
(
[3] => Orange6
[0] => Orange9
[2] => Orange10
[1] => Orange11
[4] => Orange15
)