I l@ve RuBoard Previous Section Next Section

arsort

bool arsort(array array, [int flag]) 

array

Array to be sorted

flag

Flag specifying how to compare element values (PHP 4.0.0+ only)

Sorts the given array in descending order by element value.

Returns:

TRUE on success; FALSE on error

Description:

Sorts the elements of the array given by array in descending order by element value while maintaining the key/value associations. This function differs from rsort() in that it works with both associative and indexed arrays.

This function operates directly on the passed array.

After calling arsort(), the internal array pointer is on the first element of array .

(PHP 4.0.0+ only) The way in which element values are compared can be modified by passing one of the following named constants as flag :

SORT_REGULAR

Compare element values according to PHP's normal comparison rules. This is the default if flag is not given.

SORT_NUMERIC

Compare element values according to their numeric values.

SORT_STRING

Compare element values according to their string values.

Version:

PHP 3, PHP 4

See also:

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

Example:

Sort an array in reverse order by value
echo "Unsorted:\n"; 
$my_array = array("x" => "c", "z" => "a", "y" => "d", "w" => "b"); 
echo "Unsorted:\n"; 
while (list($key, $val) = each($my_array)) {
   echo "Key: $key Val: $val\n"; 
} 
arsort($my_array); 
echo "Sorted:\n"; 
while (list($key, $val) = each($my_array)) {
   echo "Key: $key -- Val: $val\n"; 
} 

Output: 
Unsorted: 
Key: x -- Val: c 
Key: z -- Val: a 
Key: y -- Val: d 
Key: w -- Val: b 
Sorted: 
Key: y -- Val: d 
Key: x -- Val: c 
Key: w -- Val: b 
Key: z -- Val: a 
    I l@ve RuBoard Previous Section Next Section