I l@ve RuBoard Previous Section Next Section

ksort

void ksort(array array, [int flag]) 

array

Array to be sorted

flag

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

Sorts an array in ascending order by key.

Returns:

TRUE on success; NULL on failure

Description:

Performs an ascending sort based on the keys of the array rather than the values of the elements. Maintains the key/value relationships in the 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 keys according to PHP's normal comparison rules. This is the default if flag is not given.

SORT_NUMERIC

Compare element keys according to their numeric values.

SORT_STRING

Compare element keys according to their string values.

Version:

PHP 3, PHP 4

See also:

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

Example:

Sort an array in ascending order by key
echo "Unsorted:\n"; 
$my_array = array('x' => 'a', 'z' => 'b', 'y' => 'c', 'w' => 'd'); 
print_r($my_array); 
ksort($my_array); 
echo "\nSorted:\n"; 
print_r($my_array); 

Output: 
Unsorted: 
Array 
(
    [x] => a 
    [z] => b 
    [y] => c 
    [w] => d 
) 
Sorted: 
Array 
(
    [w] => d 
    [x] => a 
    [y] => c 
    [z] => b 
) 
    I l@ve RuBoard Previous Section Next Section