ksort()
In this article, we will discuss the ksort() function in PHP. The ksort() function is used to sort an array by key in ascending order. We will go through the
PHP Function: Array ksort()
The PHP ksort() function sorts an array by its keys in ascending order, keeping each key associated with its original value. It is the key-based counterpart to asort(), which sorts by value, and the opposite of krsort(), which sorts keys in descending order.
This page covers when to reach for ksort(), its syntax, parameters and return value, the available sort-type flags, and several worked examples (including the common gotcha of sorting numeric string keys).
When to use ksort()
Use ksort() when the keys carry meaning and you want the array to be ordered by them — for example:
- Sorting an associative array of
name => valuepairs alphabetically by name. - Putting configuration entries or HTTP headers into a predictable, deterministic order.
- Ordering data keyed by year, ID, or category before iterating or rendering it.
Because ksort() preserves the key/value association, it is the right tool when reindexing the array (as plain sort() would do) would lose information. See the Sorting Arrays overview for a comparison of all the PHP sort functions.
Syntax
ksort($array, $sorting_type);Parameters
The ksort() function takes two parameters:
| Parameter | Required | Description |
|---|---|---|
$array | Yes | The array to be sorted. It is passed by reference and modified in place. |
$sorting_type | No | A flag that controls how keys are compared (see the table below). Defaults to SORT_REGULAR. |
The $sorting_type flag accepts the following constants:
| Flag | Behavior |
|---|---|
SORT_REGULAR | Compare items normally (default). Numeric strings are compared as numbers. |
SORT_NUMERIC | Compare items numerically. |
SORT_STRING | Compare items as strings. |
SORT_NATURAL | Compare items as strings using "natural ordering" (like natsort()). |
SORT_LOCALE_STRING | Compare items as strings, based on the current locale. |
Note: ksort() modifies the original array in place — it does not return a new sorted array. The original key order is lost after the call.
Return Value
The ksort() function returns a boolean: true on success and false on failure.
Example 1: Sorting an Associative Array by Key
Here the keys are people's names, so ksort() orders the array alphabetically by name (Ben, Joe, Peter) while keeping each name paired with the correct age:
Output:
Array
(
[Ben] => 37
[Joe] => 43
[Peter] => 35
)Example 2: Sorting an Associative Array by Key in Reverse Order
For descending key order, use krsort() instead — it works exactly like ksort() but reverses the result:
Output:
Array
(
[Peter] => 35
[Joe] => 43
[Ben] => 37
)Example 3: Sorting an Indexed Array by Key
An indexed array already has keys 0, 1, 2, 3 in order, so sorting it by key leaves the elements unchanged. This shows that ksort() sorts keys, not values — to sort the values themselves, use sort():
Output:
Array
(
[0] => red
[1] => green
[2] => blue
[3] => yellow
)Example 4: Sorting an Indexed Array by Key in Reverse Order
Reversing the key order of an indexed array with krsort() keeps each value with its original numeric key, so the elements appear in reverse insertion order:
Output:
Array
(
[3] => yellow
[2] => blue
[1] => green
[0] => red
)Gotcha: numeric string keys and the sort flag
A common surprise is sorting an array whose keys are numeric strings. By default (SORT_REGULAR) PHP compares them as numbers, so "10" comes after "2". Passing SORT_STRING compares them character by character, putting "10" before "2":
<?php
$data = ["10" => "a", "1" => "b", "2" => "c"];
ksort($data); // SORT_REGULAR (numeric)
print_r($data);
ksort($data, SORT_STRING); // string comparison
print_r($data);Output:
Array
(
[1] => b
[2] => c
[10] => a
)
Array
(
[1] => b
[10] => a
[2] => c
)Choose the $sorting_type flag that matches how you want the keys interpreted.
Conclusion
The ksort() function sorts a PHP array by its keys in ascending order, in place, while preserving each key/value pair. Reach for it when the keys are meaningful and order matters; use krsort() for descending order, asort() / arsort() to sort by value while keeping keys, or sort() when you want to discard the keys entirely. Pick the right $sorting_type flag — especially when your keys are numeric strings — to get the comparison you expect.
graph TD
A[PHP array] -->|ksort| B[Sorted PHP array]