PHP uksort() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, uksort() sorts an array by its keys using your own comparison function, and keeps each key linked to its value.

The "u" stands for user-defined and "k" stands for key. Use it when the normal key sorting from ksort() does not fit, for example when keys must follow a custom order such as low, medium, high.

Syntax

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

PHP
uksort($fruits, fn($a, $b) => strlen($a) <=> strlen($b));
CallWhat it does
uksort($fruits, fn($a, $b) => strlen($a) <=> strlen($b))Sorts the keys from shortest to longest.
uksort($data, "strcasecmp")Sorts the keys alphabetically and ignores the case.
uksort($tasks, fn($a, $b) => $order[$a] <=> $order[$b])Sorts the keys by a custom order table.

The callback receives two keys and must return a negative number, zero or a positive number. uksort() changes the original array and returns true.

Library declarations
PHP
uksort(array &$array, callable $callback): true

Parameters

ParameterDescription
arrayRequired. The array to sort. It is passed by reference and changed in place.
callbackRequired. A function that takes two keys and returns an integer less than, equal to or greater than zero. This sets the order.

Return Value

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

PHP Examples

Example 1 — Sort Keys by Length

The callback compares the length of two keys, so the shortest key comes first.

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

uksort($fruits, fn($a, $b) => strlen($a) <=> strlen($b));

foreach ($fruits as $name => $count) {
    echo $name, " => ", $count, PHP_EOL;
}

How It Works

  1. The keys banana, fig and apple have 6, 3 and 5 letters.
  2. strlen() gives the length of each key inside the callback.
  3. <=> compares the two lengths, so the shortest key goes first.
  4. The values stay attached to their own keys.

Example 2 — Sort Keys Without Caring About Case

strcasecmp() compares two strings and ignores uppercase or lowercase letters.

PHP
<?php
$letters = ["b" => 1, "A" => 2, "c" => 3];

uksort($letters, "strcasecmp");

echo implode(", ", array_keys($letters)), PHP_EOL;

How It Works

  1. The keys are b, A and c with mixed capitals.
  2. uksort() passes two keys at a time to strcasecmp().
  3. strcasecmp() treats "A" and "a" as equal letters, so the order is alphabetical.
  4. The result is A, b, c. A plain ksort() would put every capital letter before every lowercase letter, which is often not what you want.

Example 3 — Use a Custom Order

A lookup table can describe any order you want, such as priority levels.

PHP
<?php
$order = ["low" => 1, "medium" => 2, "high" => 3];
$tasks = ["high" => "Fix bug", "low" => "Clean desk", "medium" => "Write docs"];

uksort($tasks, fn($a, $b) => $order[$a] <=> $order[$b]);

foreach ($tasks as $level => $task) {
    echo $level, ": ", $task, PHP_EOL;
}

How It Works

  1. $order gives every key a number that shows where it should appear.
  2. The arrow function looks up the numbers for the two keys it receives.
  3. <=> compares the numbers, so low (1) comes before medium (2) and high (3).
  4. The task texts move together with their keys.

Frequently Asked Questions

It sorts an array by its keys with a comparison function you provide and keeps every key linked to its value.
ksort() uses built-in comparison rules. uksort() lets you define the rule with a callback.
Two keys from the array, not values. It must return a negative number, zero or a positive number.
Use uasort() to keep the keys or usort() to renumber them.
No. It changes the array you pass in and returns true.

Next: usort()

usort() sorts the values of an array using your own comparison function and gives them new.

usort() 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