Skip to content

PHP Sorting Arrays

PHP provides powerful built-in functions to sort the elements within an array. You can sort them alphabetically or numerically, in ascending or descending order, and even maintain key associations in associative arrays.

Understanding how to sort arrays is fundamental for organizing and presenting data effectively in your applications.

PHP offers several functions tailored for different sorting needs. We will explore the most common ones:

  • sort() - Sorts an indexed array in ascending order (lowest to highest). Loses original keys.
  • rsort() - Sorts an indexed array in descending order (highest to lowest). Loses original keys.
  • asort() - Sorts an associative array in ascending order, based on the value. Preserves keys.
  • ksort() - Sorts an associative array in ascending order, based on the key. Preserves keys.
  • arsort() - Sorts an associative array in descending order, based on the value. Preserves keys.
  • krsort() - Sorts an associative array in descending order, based on the key. Preserves keys.

Important Note: These functions modify the original array directly (they sort ‘in-place’). They return true on success or false on failure, not the sorted array itself.

Sort Indexed Array in Ascending Order - sort()

Section titled “Sort Indexed Array in Ascending Order - sort()”

The sort() function is ideal for simple indexed arrays where you don’t need to preserve the original numeric keys.

Example: Sorting strings alphabetically:

<?php
$cars = ["Volvo", "BMW", "Toyota", "Audi"];
sort($cars); // Sorts the $cars array alphabetically
// Output: Array ( [0] => Audi [1] => BMW [2] => Toyota [3] => Volvo )
print_r($cars);
?>

Example: Sorting numbers numerically:

<?php
$numbers = [4, 6, 2, 22, 11];
sort($numbers); // Sorts the $numbers array numerically
// Output: Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 11 [4] => 22 )
print_r($numbers);
?>

Tip: You can provide optional flags to sort() to control the comparison behavior (e.g., SORT_NUMERIC, SORT_STRING, SORT_NATURAL for natural order sorting like ‘img1’, ‘img2’, ‘img10’). See the PHP documentation for details.

Sort Indexed Array in Descending Order - rsort()

Section titled “Sort Indexed Array in Descending Order - rsort()”

rsort() works like sort() but reverses the order.

Example: Sorting strings in reverse alphabetical order:

<?php
$cars = ["Volvo", "BMW", "Toyota", "Audi"];
rsort($cars);
// Output: Array ( [0] => Volvo [1] => Toyota [2] => BMW [3] => Audi )
print_r($cars);
?>

Example: Sorting numbers in descending numerical order:

<?php
$numbers = [4, 6, 2, 22, 11];
rsort($numbers);
// Output: Array ( [0] => 22 [1] => 11 [2] => 6 [3] => 4 [4] => 2 )
print_r($numbers);
?>

Sort Associative Array by Value (Ascending) - asort()

Section titled “Sort Associative Array by Value (Ascending) - asort()”

When working with associative arrays (key-value pairs), you often want to maintain the relationship between keys and values after sorting. asort() sorts by value but keeps the keys associated with their original values.

Example: Sorting users by age (ascending):

<?php
$userAges = ["Peter" => 35, "Ben" => 37, "Joe" => 43, "Anna" => 32];
asort($userAges); // Sorts by age (value), keeping names (keys)
// Output: Array ( [Anna] => 32 [Peter] => 35 [Ben] => 37 [Joe] => 43 )
print_r($userAges);
?>

Sort Associative Array by Key (Ascending) - ksort()

Section titled “Sort Associative Array by Key (Ascending) - ksort()”

ksort() sorts an associative array based on its keys, preserving the key-value associations.

Example: Sorting users alphabetically by name (key):

<?php
$userAges = ["Peter" => 35, "Ben" => 37, "Joe" => 43, "Anna" => 32];
ksort($userAges); // Sorts by name (key)
// Output: Array ( [Anna] => 32 [Ben] => 37 [Joe] => 43 [Peter] => 35 )
print_r($userAges);
?>

Sort Associative Array by Value (Descending) - arsort()

Section titled “Sort Associative Array by Value (Descending) - arsort()”

arsort() is the descending counterpart to asort(). It sorts by value in descending order while preserving keys.

Example: Sorting users by age (descending):

<?php
$userAges = ["Peter" => 35, "Ben" => 37, "Joe" => 43, "Anna" => 32];
arsort($userAges); // Sorts by age (value), descending
// Output: Array ( [Joe] => 43 [Ben] => 37 [Peter] => 35 [Anna] => 32 )
print_r($userAges);
?>

Sort Associative Array by Key (Descending) - krsort()

Section titled “Sort Associative Array by Key (Descending) - krsort()”

krsort() is the descending counterpart to ksort(). It sorts by key in descending order while preserving keys.

Example: Sorting users in reverse alphabetical order by name (key):

<?php
$userAges = ["Peter" => 35, "Ben" => 37, "Joe" => 43, "Anna" => 32];
krsort($userAges); // Sorts by name (key), descending
// Output: Array ( [Peter] => 35 [Joe] => 43 [Ben] => 37 [Anna] => 32 )
print_r($userAges);
?>

For more complex scenarios, such as sorting objects or using custom logic, PHP provides user-defined sort functions:

  • usort(): Sorts an array using a user-defined comparison function.
  • uasort(): Sorts an array with a user-defined comparison function, preserving keys.
  • uksort(): Sorts an array by keys using a user-defined comparison function.

For locale-aware string comparison (sorting strings correctly across different languages and character sets), consider using the Collator class available in the Intl extension.

For a comprehensive list of all array functions, including detailed descriptions and examples, refer to the official PHP Array Reference.