How to Sort Numeric Array in PHP?
Last Updated :
15 Jul, 2024
Given a numeric array, the task is to sort the numeric array using PHP. There are various built-in functions to sort arrays in different ways.
Below are the approaches to sort numeric arrays in PHP:
Using sort() Function
The sort() function sorts an array in ascending order. It sorts the values without maintaining the original keys.
Example: This example shows the implementation of the above-explained approach.
PHP
<?php
// Create an Array
$arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
// Function to sort array
sort($arr);
print_r($arr);
?>
Output
Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 3
[4] => 3
[5] => 4
[6] => 5
[7] => 5
[8] => 5
[9] => 6
[10] => 9
)
Using rsort() Function
The rsort() function sorts an array in descending order. Like sort(), it does not maintain the original keys.
Example: This example shows the implementation of the above-explained approach.
PHP
<?php
// Create an Array
$arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
// Function to sort array in
// descending order
rsort($arr);
print_r($arr);
?>
OutputArray
(
[0] => 9
[1] => 6
[2] => 5
[3] => 5
[4] => 5
[5] => 4
[6] => 3
[7] => 3
[8] => 2
[9] => 1
[10] => 1
)
Using asort() Function
The asort() function sorts an array in ascending order while maintaining the original keys.
Example: This example shows the implementation of the above-explained approach.
PHP
<?php
// Create an Array
$arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
// Function to sort array in
// ascending order without
// changing the key
asort($arr);
print_r($arr);
?>
Output
Array
(
[1] => 1
[3] => 1
[6] => 2
[0] => 3
[9] => 3
[2] => 4
[4] => 5
[8] => 5
[10] => 5
[7] => 6
[5] => 9
)
Using arsort() Function
The arsort() function sorts an array in descending order while maintaining the original keys.
Example: This example shows the implementation of the above-explained approach.
PHP
<?php
// Create an Array
$arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
// Function to sort array in
// descending order without
// changing the key
arsort($arr);
print_r($arr);
?>
OutputArray
(
[5] => 9
[7] => 6
[4] => 5
[8] => 5
[10] => 5
[2] => 4
[0] => 3
[9] => 3
[6] => 2
[1] => 1
[3] => 1
)
Using usort() Function
The usort() function sorts an array by values using a user-defined comparison function. This is useful when you need a custom sorting logic.
Example: This example shows the implementation of the above-explained approach.
PHP
<?php
// Create an Array
$arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
// Compare function
function customCompare($a, $b) {
return $a - $b;
}
usort($arr, "customCompare");
print_r($arr);
?>
Output
Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 3
[4] => 3
[5] => 4
[6] => 5
[7] => 5
[8] => 5
[9] => 6
[10] => 9
)
Using natsort()
The natsort() function sorts an array using a natural order algorithm.
Example: This example shows the implementation of the above-explained approach.
PHP
<?php
$array = array(3, 2, 9, 7, 5, 1, 4);
natsort($array);
print_r($array);
?>
OutputArray
(
[5] => 1
[1] => 2
[0] => 3
[6] => 4
[4] => 5
[3] => 7
[2] => 9
)
Explore
Basics
Array
OOPs & Interfaces
MySQL Database
PHP Advance