How to use asort() Function In PHP
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
// 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
)
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