Skip to main content

PHP - Array Sorting Functions

PHP can put array items in order, either alphabetically or by number, and in either direction: ascending (low to high) or descending (high to low).

Six functions cover the usual cases:

  • sort() - orders an indexed array from low to high
  • rsort() - orders an indexed array from high to low
  • asort() - orders an associative array from low to high by value
  • ksort() - orders an associative array from low to high by key
  • arsort() - orders an associative array from high to low by value
  • krsort() - orders an associative array from high to low by key

PHP sort() - Sort Array in Ascending Order

Call sort() on an indexed array to arrange its items in ascending order.

Sort strings in ascending order

Put the names in $cars in alphabetical order, A to Z:

Prompt
Write a PHP script that creates an array with "Volvo", "BMW", "Toyota", sorts it in ascending alphabetical order using sort(), and displays the result with print_r().
Output: PHP script

Sort numbers in ascending order

Put the values in $numbers in order, smallest first:

Prompt
Write a PHP script that creates an array with 4, 6, 2, 22, 11, sorts it in ascending numerical order using sort(), and displays the result with print_r().
Output: PHP script

PHP rsort() - Sort Array in Descending Order

rsort() does the opposite, arranging an indexed array in descending order.

Sort strings in descending order

Put the names in $cars in reverse alphabetical order, Z to A:

Prompt
Write a PHP script that creates an array with "Volvo", "BMW", "Toyota", sorts it in descending alphabetical order using rsort(), and displays the result with print_r().
Output: PHP script

Sort numbers in descending order

Put the values in $numbers in order, largest first:

Prompt
Write a PHP script that creates an array with 4, 6, 2, 22, 11, sorts it in descending numerical order using rsort(), and displays the result with print_r().
Output: PHP script

PHP asort() and arsort() - Sort Associative Array (value)

asort() orders an associative array by its values, lowest first, and each key stays attached to its value.

Sort associative array ascending by value

Order the $age array from the youngest person to the oldest:

Prompt
Write a PHP script that creates an associative array with "Peter"=>"35", "Ben"=>"37", "Joe"=>"43", sorts it in ascending order by value using asort(), and displays the result with print_r().
Output: PHP script

Sort associative array descending by value

arsort() goes the other way and orders by value from highest to lowest, as this oldest-first list shows:

Prompt
Write a PHP script that creates an associative array with "Peter"=>"35", "Ben"=>"37", "Joe"=>"43", sorts it in descending order by value using arsort(), and displays the result with print_r().
Output: PHP script

PHP ksort() and krsort() - Sort Associative Array (key)

To order by the keys rather than the values, use ksort(), which arranges an associative array by key, ascending.

Sort associative array ascending by key

Order the $age array alphabetically by name:

Prompt
Write a PHP script that creates an associative array with "Peter"=>"35", "Ben"=>"37", "Joe"=>"43", sorts it in ascending order by key using ksort(), and displays the result with print_r().
Output: PHP script

Sort associative array descending by key

krsort() reverses that, ordering by key in descending order, so the names run from Z to A:

Prompt
Write a PHP script that creates an associative array with "Peter"=>"35", "Ben"=>"37", "Joe"=>"43", sorts it in descending order by key using krsort(), and displays the result with print_r().
Output: PHP script