ksort
Sorts an array in place by its keys in ascending order, keeping key/value pairs intact.
ksort(array &$array, int $flags = SORT_REGULAR): true- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | The array to sort. Passed by reference, so it is modified directly. |
$flagsoptional | int= SORT_REGULAR | Comparison mode: SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING or SORT_NATURAL, optionally combined with SORT_FLAG_CASE for case-insensitive string or natural comparison. |
Return value
trueAlways returns true. The array is modified in place through the reference.
Errors
Nothing. Passing a non-array first argument is a TypeError, since the parameter is typed and by reference.
Changelog
- PHP 8.2The return type is true now; previously it was bool.
- PHP
See also
ksort reorders an array by its keys, smallest first, and leaves every key attached to its own value. Unlike sort, nothing is renumbered and nothing is lost — it is the safe way to put an associative array into a predictable order.
When to reach for it
Use it whenever the order of keys is what a human or a diff will see: config arrays before dumping them, date-keyed buckets like '2026-01' before charting, or two arrays you want to compare byte-for-byte after json_encode. Because JSON objects serialise in insertion order, sorting keys first makes output reproducible.
What trips people up
The default SORT_REGULAR compares keys by PHP's normal rules, and array keys are already coerced — '10' became the integer 10 the moment you wrote it. So a mix of integer-like and word keys sorts integers against strings, and in PHP 8 an integer is always less than a non-numeric string. If your keys look like item2, item10, plain sorting puts item10 first; pass SORT_NATURAL (or use uksort with strnatcmp) to get human ordering.
For descending key order use krsort. To sort by value instead, use asort.
Example
When to use it
- Put a configuration or environment array into a stable order before writing it to a file, so diffs stay small.
- Sort month- or date-keyed totals like '2026-01' => 940 before rendering a report table.
- Normalise two arrays with ksort so a strict === comparison ignores insertion order.
Frequently asked questions
What is the difference between ksort and asort?
ksort orders the array by its keys; asort orders it by its values. Both keep each key bound to its value, unlike sort, which throws the keys away.
Does ksort return the sorted array?
No — it returns true and sorts the array in place through the reference parameter. Writing $sorted = ksort($arr) gives you the boolean true, not an array.
How do I sort keys like file2, file10 in natural order?
Pass SORT_NATURAL as the second argument: ksort($arr, SORT_NATURAL). Add SORT_FLAG_CASE to ignore case, or use uksort($arr, 'strnatcmp') for the same result with a callback.
More examples
Descending order and value sorting compared
ksort gives alpha,bravo,charlie; krsort reverses that to charlie,bravo,alpha; asort ignores keys entirely and orders by value, giving charlie,bravo,alpha because 1 < 5 < 9. All three keep the key/value pairing.
<?php
$hits = ['bravo' => 5, 'alpha' => 9, 'charlie' => 1];
ksort($hits);
echo 'ksort: ', implode(',', array_keys($hits)), "\n";
krsort($hits);
echo 'krsort: ', implode(',', array_keys($hits)), "\n";
asort($hits);
echo 'asort: ', implode(',', array_keys($hits)), "\n";Stable JSON output
The first var_dump prints false: the two arrays hold identical data but serialise in different orders. After ksort on both, the encoded strings match and the second var_dump prints true.
<?php
$a = ['name' => 'kit', 'id' => 7, 'tags' => ['x']];
$b = ['id' => 7, 'tags' => ['x'], 'name' => 'kit'];
var_dump(json_encode($a) === json_encode($b));
ksort($a);
ksort($b);
var_dump(json_encode($a) === json_encode($b));
echo json_encode($a), "\n";
Discussion