Skip to content

Sorting Arrays

Introduction to PHP Arrays

In PHP, an array is a collection of elements that are stored and accessed using an index or a key. Arrays are a fundamental data structure in PHP, and they are widely used in programming. PHP provides several built-in functions to work with arrays, including sorting functions. Sorting is the process of arranging the elements in an array in a particular order.

Understanding PHP Array Sorting

PHP provides several sorting functions that can be used to sort arrays. The most commonly used sorting functions in PHP are sort(), rsort(), asort(), arsort(), ksort(), krsort(), natsort(), natcasesort(), and usort(). Each of these functions sorts an array in a different way. Let's take a closer look at the most commonly used sorting functions.

Sorting Arrays in Ascending Order

The sort() function is used to sort an array in ascending order. It sorts string elements alphabetically and numeric elements numerically. Note that sort() modifies the original array in-place and returns true on success or false on failure. When sorting mixed data types, PHP may produce unexpected results, so ensure your array contains consistent types before sorting.

php
$fruits = ['lemon', 'orange', 'banana', 'apple'];
sort($fruits);
print_r($fruits);
// Output: Array ( [0] => apple [1] => banana [2] => lemon [3] => orange )

Sorting Arrays in Descending Order

The rsort() function is used to sort an array in descending order. Like sort(), it modifies the array in-place and returns a boolean value. It reverses the alphabetical or numerical order.

php
$numbers = [5, 2, 9, 1, 7];
rsort($numbers);
print_r($numbers);
// Output: Array ( [0] => 9 [1] => 7 [2] => 5 [3] => 2 [4] => 1 )

Sorting Associative Arrays by Value

The asort() function is used to sort an associative array by value in ascending order while maintaining the association between keys and values. It modifies the array in-place and returns true or false.

php
$ages = ['Peter' => 35, 'John' => 28, 'Mary' => 32];
asort($ages);
print_r($ages);
// Output: Array ( [John] => 28 [Mary] => 32 [Peter] => 35 )

Sorting Associative Arrays by Key

The ksort() function is used to sort an associative array by key in ascending order, while maintaining the association between keys and values. It also modifies the array in-place and returns a boolean.

php
$colors = ['red' => '#FF0000', 'blue' => '#0000FF', 'green' => '#008000'];
ksort($colors);
print_r($colors);
// Output: Array ( [blue] => #0000FF [green] => #008000 [red] => #FF0000 )

Natural Order and Custom Sorting

For specialized sorting requirements, PHP provides additional functions:

  • natsort() sorts arrays using a "natural order" algorithm, which treats numeric strings numerically (e.g., file2 sorts before file10). It modifies the array in-place.
  • natcasesort() performs a case-insensitive natural order sort.
  • usort() allows you to define a custom comparison function using usort($array, $callback). This is essential for sorting complex data structures, objects, or multi-dimensional arrays.

Conclusion

Sorting arrays in PHP is a common task in programming, and PHP provides several built-in functions to make it easy. In this guide, we have covered the most commonly used sorting functions in PHP and how they can be used to sort arrays in different ways. By using these sorting functions, you can easily sort arrays in ascending or descending order, as well as sort associative arrays by value or key. We hope that this guide has been informative and helpful, and we wish you the best of luck in your PHP programming endeavors!

Practice

Which of the following are valid sorting functions in PHP according to the information on the provided URL?

Dual-run preview — compare with live Symfony routes.