PHP » Arrays » ksort()

Syntax:
bool ksort (array array [, int sort_type])
array
Array to be sorted.
sort_type
Can be SORT_REGULAR, SORT_NUMERIC, or SORT_STRING.

Sorts an array in ascending order by key.

ksort() orders the elements by key, rather than the value of the elements. The optional sort_type parameter specifies whether the elements should be sorted as strings or numerically. If the array was sorted successfully, TRUE is returned.

Examples

Code:
<pre>
<?php

$cities = array(
   "Miami" => "Florida",
   "New Orleans" => "Louisiana",
   "Los Angeles" => "California",
   "Atlanta" => "Georgia",
   "Memphis" => "Tennessee");

ksort($cities);
print_r($cities);

?>
</pre>
Output:
Array
(
   [Atlanta] => Georgia
   [Los Angeles] => California
   [Memphis] => Tennessee
   [Miami] => Florida
   [New Orleans] => Louisiana
)
Explanation:

An array with cities as keys and states as values is sorted with ksort().

See Also: