I l@ve RuBoard Previous Section Next Section

uasort

bool uasort(array array, string func) 

array

Array to be sorted

func

Function to use for sorting

Sorts an array by value with a user-defined function.

Returns:

TRUE on success; FALSE on failure

Description:

uasort() sorts array by the value of each element, using the specified user-defined function named by func . Key/value relationships are preserved. This function is useful if the values of an associative array must be in some specific non-regular order, such as when you want to do a non-case-sensitive sort with both uppercase and lowercase alphabets.

func should be written to accept two parameters, which it must compare with each other. If they're considered equivalent, 0 should be returned. If the first is considered to be greater, 1 should be returned. If the second is considered to be greater, -1 should be returned.

Version:

PHP 3 since 3.0.4, PHP 4

See also:

array_multisort() 
arsort() 
asort() 
krsort() 
ksort() 
natsort() 
natcasesort() 
rsort() 
sort() 
uksort() 
usort() 

Example:

Sort an array by value with a user-defined comparison function
function uasort_cmp($a, $b) { // Sort in ascending order 
   if (strtolower($a) == strtolower($b)) return 0; 
   return (strtolower($a) > strtolower($b)) ? 1 : -1; 
} 
$arr = array("tom" => "Apple", "janet" => "apple", 
             "fred" => "Banana", "barney" => "banana", 
             "bullwinkle "=> "Carrot", "rocky" => "carrot; 
uasort($arr, "uasort_cmp"); 
while(list($k, $v) = each($arr)) {
   echo "$k => $v\n"; 
} 

Output: 
tom => Apple 
janet => apple 
fred => Banana 
barney => banana 
bullwinkle => Carrot 
rocky => carrot 
    I l@ve RuBoard Previous Section Next Section