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: 1561 times

What is PHP?

PHP is a scripting language for web development with open source 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 associative arrays in ascending order

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
#)

Sorting associative arrays in descending order

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

// try get IpInfo ASAP! getIpInfo($.noop);