php8.5
Home/ Manual/ array / functions/ asort

asort

PHP function Edit on GitHub ✎

(PHP 4, PHP 5, PHP 7, PHP 8)

Sort an array in ascending order and maintain index association

Description

asort(array $array, int $flags = SORT_REGULAR): true

Sorts array in place in ascending order, such that its keys maintain their correlation with the values they are associated with.

This is used mainly when sorting associative arrays where the actual element order is significant.

Sort-unstable Reset-index

Parameters

array

The input array.

Return Values

Always

Changelog

VersionDescription

Examples

asort() example

php
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

The above example will output:

output
c = apple
b = banana
d = lemon
a = orange

The fruits have been sorted in alphabetical order, and the index associated with each element has been maintained.

See Also

Source: reference/array/functions/asort.xml · from the official PHP manual (php/doc-en)