sort
Sorts an array by value in ascending order and reindexes it with fresh numeric keys.
sort(array &$array, int $flags = SORT_REGULAR): true- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | The array to sort. Passed by reference; existing keys are discarded and replaced with 0-based integers. |
$flagsoptional | int= SORT_REGULAR | Comparison mode: SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING or SORT_NATURAL, optionally combined with SORT_FLAG_CASE for case-insensitive comparison. |
Return value
trueAlways returns true. The array is sorted in place through the reference.
Errors
Nothing. A non-array first argument raises a TypeError from the parameter type.
Changelog
- PHP 8.2The return type is true now; previously it was bool.
- PHP 8.0The sort is stable: elements that compare as equal keep their original relative order. Numeric string comparison under SORT_REGULAR follows the PHP 8 comparison rules.
sort is the default array sort: values ascending, keys thrown away and replaced with 0, 1, 2, …. It works in place and returns true, not the sorted array.
When to reach for it
Use it when you have a plain list and want it ordered — tag names for display, numbers before computing a median, a set of IDs you are about to compare with another sorted set. Because it reindexes, the output always encodes as a JSON array, which makes it a safe last step before returning data from an API.
What trips people up
The reindexing is the classic trap. On an associative array sort silently discards the keys, so ['bo' => 95, 'ana' => 88] becomes [88, 95]. Use asort to sort by value while keeping keys, or ksort to sort by key.
The second trap is SORT_REGULAR on mixed data. PHP 8 compares a number against a numeric string numerically but against a non-numeric string as a string, so an array holding 5, '10' and 'apple' sorts consistently but not intuitively. State your intent with SORT_NUMERIC or SORT_STRING. Values read from forms, CSV files or JSON are strings, so '10' sorting before '9' is usually a missing flag rather than a bug.
For anything more involved — sorting objects, or by a field inside each row — use usort.
Example
When to use it
- Alphabetise a list of tag or category names before rendering them.
- Order numeric samples ascending so you can take the median from the middle index.
- Normalise two ID lists with sort so a === comparison ignores their original order.
Frequently asked questions
Why does sort return true instead of the sorted array?
sort modifies the array in place through its by-reference parameter and returns true to report success. Sort the variable first, then use the variable itself.
What is the difference between sort and asort?
Both order the array by value ascending, but sort discards the keys and renumbers from 0 while asort preserves the key/value pairs. Use asort for associative arrays.
How do I sort an array of arrays by one column?
Use usort with a spaceship comparison on that field, for example usort($rows, fn($a, $b) => $a['price'] <=> $b['price']), or use array_multisort with an extracted column.
More examples
Keys are destroyed — use asort instead
sort prints [4,7,12] — ordered, but you can no longer tell which fruit each count belongs to. asort prints {"pears":4,"plums":7,"apples":12}, keeping the labels attached.
<?php
$stock = ['pears' => 4, 'apples' => 12, 'plums' => 7];
$a = $stock; sort($a);
$b = $stock; asort($b);
echo 'sort: ', json_encode($a), "\n";
echo 'asort: ', json_encode($b), "\n";Case and natural ordering flags
The default byte comparison puts Item2 first because uppercase I is lower than lowercase i, then item1 and item10. SORT_NATURAL | SORT_FLAG_CASE gives item1, Item2, item10 — the order a reader expects.
<?php
$items = ['item10', 'Item2', 'item1'];
$plain = $items; sort($plain);
$nat = $items; sort($nat, SORT_NATURAL | SORT_FLAG_CASE);
echo 'default: ', implode(' ', $plain), "\n";
echo 'natural: ', implode(' ', $nat), "\n";
Discussion