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
)
Similar Reads
How to Check an Array is Sorted or Not in PHP? Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorte
3 min read
How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he
2 min read
How to Convert Number to Character Array in PHP ? Given a number, the task is to convert numbers to character arrays in PHP. It is a common operation when you need to manipulate or access individual digits of a number. This can be particularly useful in situations where you need to perform operations on the digits of a number, such as digital root
3 min read
Sort an array of dates in PHP We are given an array of multiple dates in (Y-m-d) format. We have to write a program in PHP to sort all the dates in the array in decreasing order. Examples : Input : array("2018-06-04", "2014-06-08", "2018-06-05") Output : 2018-06-05 2018-06-04 2014-06-08 Input : array("2016-09-12", "2009-09-08",
2 min read
How to Convert a Given Number to Words in PHP? Given an integer N, your task is to convert the given number into words using PHP.Examples:Input: N = 958237764Output: Nine Hundred Fifty Eight Million Two Hundred Thirty Seven Thousand Seven Hundred Sixty FourInput: N = 5000Output: Five ThousandBelow are the approaches to convert a given number to
5 min read