PHP Manual
Prev |  | Next

# ksort

ksort -- Sort an array by key

## Description

`int ksort`(array array);

Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.

Example 1. ksort() example
    $fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");
    ksort($fruits);
    for(reset($fruits); $key = key($fruits); next($fruits)) {
        echo "fruits[$key] = ".$fruits[$key]."\n";
    }

This example would display: `fruits[a] = orange fruits[b] = banana fruits[c] = apple fruits[d] = lemon`

See also asort(), arsort(), sort(), and rsort().

Prev | Home | Next
krsort | Up | list