PHP array sorting tutorial shows how to sort arrays and objects in PHP. Learn sort, usort, and more with examples.
last modified March 13, 2025
PHP provides powerful functions for sorting arrays and objects, such as sort, asort, ksort, and usort. This tutorial covers these with practical examples.
The sort function sorts an array in ascending order, re-indexing numeric keys.
basic_sort.php
<?php
declare(strict_types=1);
$numbers = [3, 1, 4, 1, 5]; sort($numbers); print_r($numbers);
This sorts $numbers in ascending order. The original array [3, 1, 4, 1, 5] becomes [1, 1, 3, 4, 5]. Keys are reset to 0 through 4.
Use sort to sort an array of strings alphabetically.
sort_strings.php
<?php
declare(strict_types=1);
$fruits = [“banana”, “apple”, “cherry”]; sort($fruits); print_r($fruits);
This sorts $fruits alphabetically. The result is [“apple”, “banana”, “cherry”], with keys re-indexed from 0.
The asort function sorts by value, keeping key associations.
asort_example.php
<?php
declare(strict_types=1);
$scores = [“John” => 85, “Jane” => 92, “Bob” => 78]; asort($scores); print_r($scores);
This sorts $scores by value. The result is [“Bob” => 78, “John” => 85, “Jane” => 92], with keys preserved.
The ksort function sorts an array by key in ascending order.
ksort_example.php
<?php
declare(strict_types=1);
$data = [“z” => 1, “x” => 2, “y” => 3]; ksort($data); print_r($data);
This sorts $data by key. The result is [“x” => 2, “y” => 3, “z” => 1], with values tied to their original keys.
The rsort function sorts an array in descending order.
rsort_example.php
<?php
declare(strict_types=1);
$numbers = [3, 1, 4, 1, 5]; rsort($numbers); print_r($numbers);
This sorts $numbers in descending order. The result is [5, 4, 3, 1, 1], with keys re-indexed from 0.
The usort function sorts using a custom comparison function.
usort_example.php
<?php
declare(strict_types=1);
$numbers = [10, 5, 8, 3]; usort($numbers, fn($a, $b): int => $b - $a); print_r($numbers);
This sorts $numbers in descending order using an arrow function. The callback returns a negative, zero, or positive value to determine order. The result is [10, 8, 5, 3].
Use usort to sort objects by a property value.
sort_objects.php
<?php
declare(strict_types=1);
class Product { public function __construct( public string $name, public float $price ) {} }
$products = [ new Product(“Phone”, 500), new Product(“Laptop”, 1000), new Product(“Tablet”, 300) ];
usort($products, fn(Product $a, Product $b): int =>
$a->price <=> $b->price);
print_r($products);
This sorts $products by price. The spaceship operator (<=>) compares prices, resulting in objects ordered as “Tablet” (300), “Phone” (500), “Laptop” (1000).
The uasort function sorts by value, preserving keys, with a custom function.
uasort_example.php
<?php
declare(strict_types=1);
$scores = [“John” => 85, “Jane” => 92, “Bob” => 78]; uasort($scores, fn($a, $b): int => $b <=> $a); print_r($scores);
This sorts $scores by value in descending order. The result is [“Jane” => 92, “John” => 85, “Bob” => 78], with keys intact.
The uksort function sorts by key using a custom function.
uksort_example.php
<?php
declare(strict_types=1);
$data = [“z” => 1, “x” => 2, “y” => 3]; uksort($data, fn(string $a, string $b): int => strlen($a) <=> strlen($b)); print_r($data);
This sorts $data by key length. The result is [“x” => 2, “y” => 3, “z” => 1], as all keys are single characters, sorted alphabetically by default tie-breaking.
Sort multidimensional arrays by a specific key with usort.
sort_multi.php
<?php
declare(strict_types=1);
$users = [ [“name” => “John”, “age” => 25], [“name” => “Jane”, “age” => 30], [“name” => “Bob”, “age” => 20] ]; usort($users, fn(array $a, array $b): int => $a[“age”] <=> $b[“age”]); print_r($users);
This sorts $users by age. The result is “Bob” (20), “John” (25), “Jane” (30), with array structure preserved.
Sort objects by multiple properties using usort.
sort_multi_criteria.php
<?php
declare(strict_types=1);
class Item { public function __construct( public string $name, public int $stock ) {} }
$items = [ new Item(“Laptop”, 5), new Item(“Phone”, 10), new Item(“Tablet”, 5) ];
usort($items, fn(Item $a, Item $b): int => $a->stock <=> $b->stock ?: $a->name <=> $b->name); print_r($items);
This sorts $items by stock, then name. The result is “Laptop” (5), “Tablet” (5), “Phone” (10), with ties broken alphabetically.
Use array_multisort to sort multiple arrays or columns.
array_multisort_example.php
<?php
declare(strict_types=1);
$names = [“John”, “Jane”, “Bob”]; $ages = [25, 30, 20]; array_multisort($ages, SORT_ASC, $names); print_r([$names, $ages]);
This sorts $ages and aligns $names. The result is $names = [“Bob”, “John”, “Jane”] and $ages = [20, 25, 30].
Sort an array of user objects by age, calculated from date of birth.
sort_users_by_age.php
<?php
declare(strict_types=1);
class User { public function __construct( public string $first_name, public string $last_name, public string $date_of_birth ) {} }
$users = [ new User(“John”, “Doe”, “2000-05-15”), new User(“Jane”, “Smith”, “1995-08-22”), new User(“Bob”, “Brown”, “1998-11-30”) ];
usort($users, fn(User $a, User $b): int => (new DateTime($a->date_of_birth)) <=> (new DateTime($b->date_of_birth))); print_r($users);
This sorts $users by birth date, oldest first. The callback compares $date_of_birth values, resulting in “Jane” (1995), “Bob” (1998), “John” (2000).
Use Appropriate Functions: Choose sort or usort based on needs. Preserve Keys: Use asort or ksort for associative arrays. Leverage Spaceship: Simplify comparisons with <=>.
This tutorial explores sorting arrays and objects in PHP with modern techniques and practical examples.
My name is Jan Bodnar, a passionate programmer with over a decade of experience. I’ve written 1400+ articles and 8 e-books since 2007.
List all PHP tutorials.