PHP ksort() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, ksort() sorts an array by its keys in ascending order and keeps the key and value pairs together.

The "k" stands for key. The array is changed in place. Use it when your keys have meaning, such as names or IDs, and you want the array in key order.

Syntax

Call ksort() with the values you need. Start with this line.

PHP
ksort($stock);
CallWhat it does
ksort($stock)Sorts the pairs from the lowest key to the highest key.
ksort($stock, SORT_STRING)Compares the keys as text.
krsort($stock)The descending version of the same sort.

ksort() changes the original array and returns true. Every value keeps its own key. To sort by value and keep the keys, use asort().

Library declarations
PHP
ksort(array &$array, int $flags = SORT_REGULAR): true

Parameters

ParameterDescription
arrayRequired. The array to sort. It is passed by reference and changed in place.
flagsOptional. How to compare the keys. SORT_REGULAR (default), SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING, SORT_NATURAL, and SORT_FLAG_CASE combined with SORT_STRING or SORT_NATURAL.

Return Value

Return ValueDescription
trueAlways returns true. The sorted result is stored in the array you passed in.

PHP Examples

Example 1 — Sort Keys from A to Z

The pairs are rearranged so the keys are in alphabetical order.

PHP
<?php
$stock = ["banana" => 3, "apple" => 1, "cherry" => 2];

ksort($stock);

foreach ($stock as $fruit => $count) {
    echo $fruit, " => ", $count, PHP_EOL;
}

How It Works

  1. $stock has fruit names as keys, in no particular order.
  2. ksort() sorts the pairs by key in ascending order.
  3. Each count stays with its fruit name.
  4. The loop prints apple, banana and cherry in order.

Example 2 — Number Keys: Numeric or Text Order

The same keys give different orders depending on whether they are compared as numbers or as text.

PHP
<?php
$items = [10 => "ten", 2 => "two", 33 => "x"];

ksort($items);
echo implode(",", array_keys($items)), PHP_EOL;

ksort($items, SORT_STRING);
echo implode(",", array_keys($items)), PHP_EOL;

How It Works

  1. With the default flag the integer keys are compared as numbers, giving 2, 10, 33.
  2. array_keys() and implode() turn the keys into one readable line.
  3. SORT_STRING compares the keys as text character by character.
  4. As text, "10" comes before "2" because "1" is smaller than "2", so the order is 10, 2, 33.

Example 3 — Uppercase Versus Case-Insensitive Order

By default, capital letters come before lowercase letters. SORT_FLAG_CASE ignores the case.

PHP
<?php
$first = ["b" => 1, "B" => 2, "a" => 3];
ksort($first);
echo implode(",", array_keys($first)), PHP_EOL;

$second = ["b" => 1, "B" => 2, "a" => 3];
ksort($second, SORT_FLAG_CASE | SORT_STRING);
echo implode(",", array_keys($second)), PHP_EOL;

How It Works

  1. In the default order, "B" (a capital) is smaller than "a" and "b", so the result is B, a, b.
  2. The second array has the same keys but uses SORT_FLAG_CASE | SORT_STRING.
  3. Now the case is ignored, so a comes first and the letter b comes next.
  4. "b" and "B" count as equal, so PHP 8 keeps their original order and gives a, b, B.

Frequently Asked Questions

It sorts an array by its keys in ascending order and keeps every key linked to its value.
ksort() sorts by key and keeps the keys. sort() sorts by value and replaces the keys with 0, 1, 2 and so on.
No. It changes the array you pass and returns true.
Use krsort(). It works like ksort() but puts the largest key first.
PHP compares strings by character code by default, and capital letters have lower codes than lowercase letters. Use SORT_FLAG_CASE | SORT_STRING to ignore the case.

Next: list()

list() is a language construct that copies the values of an array into separate variables .

list() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful