The asort function sorts an associative array in ascending order based on the values.
What is the syntax of asort function in PHP?
asort(array, sorttype)
Parameter | Description |
---|---|
array | The array to sort – Required |
sorttype | Optional. Specifies how to compare the array elements/items. SORT_STRING (default) – Compare elements as strings SORT_REGULAR – Compares normally without changing the type SORT_NUMERIC – Compare as numeric values SORT_LOCALE_STRING – Compare as a string according to the current locale |
Example of asort function
Example 1. Sort an associative array in ascending order according to its values.
<?php
$age=array("Jawad"=>"24","Ahmad"=>"25","Sumerina"=>"39");
asort($age);
?>