asort
(PHP 4, PHP 5, PHP 7, PHP 8)
asort — Sorts an array in ascending order, preserving index association.
Description
asort(array &$array, int $flags = SORT_REGULAR): true
Parameter list
array-
Input array.
flags-
The optional second parameter
flagschanges the sorting behavior and takes the following values:Sorting type flags:
SORT_REGULAR— a normal comparison of elements; details are described in the comparison operators sectionSORT_NUMERIC– numerical comparison of elementsSORT_STRING— string comparison of elementsSORT_LOCALE_STRING— compares elements as strings based on the current locale. The flag uses a locale that can be changed using the setlocale() function.SORT_NATURAL— compare elements as strings based on “natural order” like natsort()SORT_FLAG_CASE— the flag is combined via bitwise OR with the flagSORT_STRINGorSORT_NATURALto sort the strings in a case-insensitive manner
Return values
The function returns a logical value true.
Examples
Example #1 An example of sorting an array in ascending order while maintaining the relationship between keys and values using the asort() function
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
The result of executing the given example:
c = apple b = banana d = lemon a = orange