Sorting an Array in PHP

To sort the elements of a PHP array, you can use one of the following PHP functions: sort(), rsort(), asort(), ksort(), arsort(), krsort(). Arrays can be sorted alphabetically or numerically, in ascending or descending order. When sorting an associative array in PHP, array keys retain their correlation with the associated values. The sort() function sorts an array in ascending order, while rsort() sorts it in descending order. The asort() function sorts an associative array in ascending order, and the arsort() sorts in descending order. The ksort() function sorts associative arrays in ascending order by keys, and the krsort() sorts in descending order. In this PHP Sort Array example, we use the sort() function to sort the elements of an array in numerical order. Below you can see more examples of sorting PHP array elements with a detailed description of each method. Click Execute to run the PHP Sort Array Example online and see the result.
Sorting an Array in PHP Execute
<?php
$num = array(3, 1, 4, 5, 2);
sort($num);
  
print_r($num)
?>
Updated: Viewed: 5038 times

What is PHP?

PHP is an open-source scripting language for web development and can be integrated into HTML code. PHP is known for its flexibility and large developer community. PHP is compatible with several operating systems, such as Windows, Linux, and macOS, and supports various databases, including MySQL, PostgreSQL, and SQLite.

What is an array in PHP?

In PHP, an array is a variable used to store a set of data elements identified by a unique key or index (the key can be a string or a number). PHP has two types of arrays: indexed arrays and associative arrays. PHP provides several built-in functions for manipulating arrays, such as sorting, searching, checking if an element exists, splitting a string into an array, converting arrays to string or JSON, and getting the length of an array.

PHP Sort Array Examples

The following are examples of sorting a PHP array:

Sorting an array in ascending order

To sort an array in ascending order in PHP, you can use the sort() function:

PHP sort() Example
<?php
$num = array(3, 1, 4, 5, 2);
sort($num);
  
print_r($num)
?>

#output: Array
#(
#    [0] => 1
#    [1] => 2
#    [2] => 3
#    [3] => 4
#    [4] => 5
#)

Sorting an array in descending order

To sort an array in descending PHP order, you can use the rsort() function:

PHP rsort() Example
<?php
$num = array(3, 1, 4, 5, 2);
rsort($num);
  
print_r($num)
?>

#output: Array
#(
#    [0] => 5
#    [1] => 4
#    [2] => 3
#    [3] => 2
#    [4] => 1
#)

Sorting an associative array in ascending order

To sort associative arrays in ascending order of their values, you can use the asort() function:

PHP asort() Example
<?php
$age = array(
  'Alice' => '39', 
  'Leo' => '15', 
  'Jack' => '27'
);
asort($age);

print_r($age);
?>

#output: Array
#(
#    [Leo] => 15
#    [Jack] => 27
#    [Alice] => 39
#)

To sort associative arrays in ascending order of keys, you can use the ksort() function:

PHP ksort() Example
<?php
$age = array(
  'Alice' => '39', 
  'Leo' => '15', 
  'Jack' => '27'
);
ksort($age);

print_r($age)
?>

#output: Array
#(
#    [Alice] => 39
#    [Jack] => 27
#    [Leo] => 15
#)

Sorting associative arrays in descending order

To sort associative arrays in descending order of their value, you can use the arsort() function:

PHP arsort() Example
<?php
$age = array(
  'Alice' => '39', 
  'Leo' => '15', 
  'Jack' => '27'
);
arsort($age);

print_r($age)
?>

#output: Array
#(
#    [Alice] => 39
#    [Jack] => 27
#    [Leo] => 15
#)

To sort associative arrays in descending order by key, you can use the krsort() function:

PHP krsort() Example
<?php
$age = array(
  'Alice' => '39', 
  'Leo' => '15', 
  'Jack' => '27'
);
krsort($age);

print_r($age)

?>

#output: Array
#(
#    [Leo] => 15
#    [Jack] => 27
#    [Alice] => 39
#)

See also