Sorting arrays is one of the most common operations in PHP programming. It lets you order data by values or keys, which is essential in reports, product lists, rankings, filtering results, or preparing data for display in a user interface. In this lesson, you’ll learn how sorting works with PHP functions: sort, asort, and ksort. You’ll see practical code examples, best practices, and the most common mistakes to avoid.
Basics of Array Sorting in PHP
What you need to know first
- Indexed arrays have numeric indexes (0, 1, 2, ...).
- Associative arrays have string keys (e.g., "name" => "Alice").
- PHP provides different sorting functions:
All these functions:
- Work in place (modify the original array).
- Return a boolean (true on success).
- Can take additional sorting flags such as SORTNUMERIC, SORTSTRING, SORTNATURAL, SORTFLAGCASE.
PHP Code Examples
sort — sorting by values with reindexing
<?php
$numbers = [5, 2, 9, 1, 2];
sort($numbers); // sorts ascending and reindexes
print_r($numbers); // [1, 2, 2, 5, 9]
Sorting strings vs numbers:
<?php
$data = ["10", "2", "Ananas", "banana"];
// Default sorting (SORT_REGULAR):
sort($data);
print_r($data);
// "10" and "2" are strings, compared lexicographically ("10" < "2").
// Force numeric sorting:
$data = ["10", "2", "30"];
sort($data, SORT_NUMERIC);
print_r($data); // ["2", "10", "30"]
// Force string sorting (case-insensitive):
$fruits = ["banana", "Ananas", "plum"];
sort($fruits, SORT_STRING | SORT_FLAG_CASE);
print_r($fruits); // ["Ananas", "banana", "plum"]
Natural sorting (e.g., "file2" before "file10"):
<?php
$files = ["file10.txt", "file2.txt", "file1.txt"];
natsort($files); // natural value sorting, preserves keys
print_r($files);
Reverse sorting:
- sort descending:
rsort($array)
- asort descending:
arsort($array)
- ksort descending:
krsort($array)
asort — sorting by values while preserving keys
<?php
$ages = [
"Alice" => 25,
"Olek" => 19,
"Zenek" => 19,
"Bartek"=> 30,
];
asort($ages);
print_r($ages);
/*
Result:
[
"Olek" => 19,
"Zenek" => 19,
"Alice" => 25,
"Bartek"=> 30,
]
*/
Descending:
<?php
arsort($ages);
print_r($ages);
With numeric flag:
<?php
$scores = ["Alice" => "10", "Olek" => "2", "Bartek" => "30"];
asort($scores, SORT_NUMERIC);
print_r($scores);
ksort — sorting by keys
<?php
$settings = [
"db_host" => "localhost",
"app_env" => "prod",
"cache" => true,
];
ksort($settings);
print_r($settings);
String keys that look like numbers:
<?php
$data = ["10" => "X", "2" => "Y", "1" => "Z"];
ksort($data);
print_r($data); // "1","10","2" (lexicographic)
ksort($data, SORT_NUMERIC);
print_r($data); // 1,2,10
Reverse key sort:
<?php
krsort($settings);
print_r($settings);
Best Practices and Common Mistakes
Best Practices
- Decide what you are sorting:
- Pick the right sorting flag:
- Preserve the original if needed:
- Remember PHP 8+: sorting is stable (equal values keep original order).
- For proper language rules (like Polish characters), use intl’s Collator::sort/asort.
Common Mistakes
- Losing keys with sort/rsort (use asort/arsort instead).
- Wrong comparison mode: "10" vs "2" as strings → lexicographic order.
- Mixing value types: numbers + strings may behave unexpectedly.
- Assuming sort returns a sorted array (it returns bool, modifies in place).
- Overcomplicating with usort when flags suffice.
Additional Examples
Natural sort, case-insensitive:
<?php
$files = ["File10.txt", "file2.txt", "file1.txt"];
natcasesort($files);
print_r($files);
Sort keys numerically:
<?php
$map = ["2" => "B", "10" => "C", "1" => "A"];
ksort($map, SORT_NUMERIC);
print_r($map);
Multi-stage sorting (thanks to stability):
<?php
$products = [
["name" => "Bananas", "price" => 5],
["name" => "Ananas", "price" => 5],
["name" => "Plums", "price" => 7],
];
usort($products, fn($a,$b) => $a["name"] <=> $b["name"]);
usort($products, fn($a,$b) => $a["price"] <=> $b["price"]);
print_r($products);
Summary
- sort – sorts by values, reindexes (loses keys).
- asort – sorts by values, keeps keys (best for associative arrays).
- ksort – sorts by keys.
- Use correct flags: SORTNUMERIC, SORTSTRING, SORTNATURAL.
- Sorting modifies arrays in place, returns bool.
- Beware mixed types, key preservation, and misunderstanding return values.
- For locale-aware sorting: use intl’s Collator.
Mini Quiz
- Which function sorts associative array by values and keeps keys? → asort
- Which flag for numeric-like strings? → SORTNUMERIC
- What does ksort do? → Sorts by keys, preserves values
- How to sort \["file2","file10","file1"] naturally? → natsort
- Does sort() return the sorted array? → No (returns bool, sorts in place)
- How to sort associative array by keys descending? → krsort
Now you know the essentials of sorting arrays in PHP. Practice with different flags and data types to gain confidence using sort, asort, and ksort effectively.