PHP uksort Function
last modified March 13, 2025
The PHP uksort
function sorts an array by keys using a user-defined
comparison function. It maintains key-value associations during sorting.
Basic Definition
The uksort
function sorts an array by keys using a callback
comparison function. It modifies the original array and returns true on success.
Syntax: uksort(array &$array, callable $callback): bool
. The
callback compares two keys and returns an integer less than, equal to, or
greater than zero.
Basic uksort Example
This example demonstrates sorting an associative array by keys in ascending order.
<?php $data = [ "banana" => 3, "apple" => 2, "orange" => 5 ]; uksort($data, function($a, $b) { return strcmp($a, $b); }); print_r($data);
Output shows the array sorted alphabetically by keys. The callback uses
strcmp
for string comparison. Keys maintain their values.
Reverse Key Sorting
Sort array keys in descending order by modifying the comparison logic.
<?php $data = [ "banana" => 3, "apple" => 2, "orange" => 5 ]; uksort($data, function($a, $b) { return strcmp($b, $a); // Reverse comparison }); print_r($data);
This reverses the key order by swapping $a
and $b
in
strcmp
. The output shows keys from "orange" to "apple".
Numeric Key Sorting
Sort numeric keys with custom comparison logic for different sorting needs.
<?php $data = [ 10 => "ten", 2 => "two", 5 => "five" ]; uksort($data, function($a, $b) { return $a <=> $b; // Spaceship operator for numeric comparison }); print_r($data);
The spaceship operator (<=>
) provides clean numeric comparison.
Output shows keys sorted from 2 to 10 with their corresponding values.
Case-Insensitive Sorting
Sort keys case-insensitively by modifying the string comparison approach.
<?php $data = [ "Banana" => 3, "apple" => 2, "Orange" => 5 ]; uksort($data, function($a, $b) { return strcasecmp($a, $b); // Case-insensitive comparison }); print_r($data);
strcasecmp
ignores case differences. Output shows "apple" first
despite lowercase, followed by "Banana" and "Orange" in alphabetical order.
Custom Key Sorting Logic
Implement complex sorting logic based on key characteristics or patterns.
<?php $data = [ "file1.txt" => "Content 1", "file10.txt" => "Content 10", "file2.txt" => "Content 2" ]; uksort($data, function($a, $b) { $numA = intval(preg_replace('/[^0-9]/', '', $a)); $numB = intval(preg_replace('/[^0-9]/', '', $b)); return $numA <=> $numB; }); print_r($data);
This extracts numbers from filenames for natural sorting. Output shows files ordered by their numeric parts (1, 2, 10) rather than lexicographically.
Best Practices
- Consistent Returns: Ensure callback always returns integer.
- Performance: Avoid complex logic in large-array callbacks.
- Readability: Use named functions for complex comparisons.
- Type Safety: Add type hints if keys have specific types.
Source
This tutorial covered the PHP uksort
function with practical
examples showing various key sorting scenarios and custom comparison logic.
Author
List all PHP Array Functions.