How to use arsort() Function In PHP
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
// 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);
?>
Output
Array ( [5] => 9 [7] => 6 [4] => 5 [8] => 5 [10] => 5 [2] => 4 [0] => 3 [9] => 3 [6] => 2 [1] => 1 [3] => 1 )
How to Sort Numeric Array in PHP?
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:
Table of Content
- Using sort() Function
- Using rsort() Function
- Using asort() Function
- Using arsort() Function
- Using usort() Function
Contact Us