Sorting an Array in PHP

To sort the elements of a PHP array alphabetically or numerically, in ascending or descending order, you can use the sort($array, $flags), rsort($array, $flags), asort($array, $flags), ksort($array, $flags), arsort($array, $flags), krsort($array, $flags) PHP functions. The sort() function sorts arrays in ascending order. The rsort() function sorts arrays in descending order. The asort() function sorts an associative array in ascending order so that its keys retain their correlation with the values they are associated with. The ksort() function sorts associative arrays in ascending order by key. The arsort() function sorts an associative array in descending order so that its keys retain their correlation with the values they are associated with. The krsort() function sorts associative arrays in descending order by key. 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: 1070 times

How to sort an array in ascending order in PHP?

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
)

How to sort an array in descending order in PHP?

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
)

How to sort associative arrays in ascending order in PHP?

To sort associative arrays in ascending order according to their value, 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 by key, 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
)

How to sort associative arrays in descending order in PHP?

To sort associative arrays in descending order according to 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