How to use asort() Function In PHP

The asort() function is similar to sort(), but it maintains the association between keys and values.

PHP




<?php
  
// Associative array
$arr = ['b' => 5, 'a' => 2, 'd' => 8];
  
// Sort array by values in
// ascending order
asort($arr);
  
// Display the sorted array
print_r($arr);
  
?>


Output

Array
(
    [a] => 2
    [b] => 5
    [d] => 8
)

PHP Program to Sort an Array in Ascending Order

Sorting array elements is a common operation in programming. PHP provides several methods to achieve this operation. In this article, we will explore various approaches to sort array elements of an array in ascending order.

Table of Content

  • Using sort() Function
  • Using asort() Function
  • Using array_multisort() Function
  • Using uasort() Function with Custom Comparison Function

Similar Reads

Using sort() Function

The sort() function is a built-in PHP function that sorts an array in ascending order....

Using asort() Function

...

Using array_multisort() Function

The asort() function is similar to sort(), but it maintains the association between keys and values....

Using uasort() Function with Custom Comparison Function

...