PHP asort() Function

The asort() function sorts an associative array in ascending order, according to the value.

Tip: Use the arsort() function to sort an associative array in descending order, according to the value.

Tip: Use the ksort() function to sort an associative array in ascending order, according to the key.

ParameterDescription
arrayRequired. Specifies the array to sort
sortingtypeOptional. Specifies how to compare the array elements/items. Possible values:0 = SORT_REGULAR – Default. Compare items normally (don’t change types)1 = SORT_NUMERIC – Compare items numerically2 = SORT_STRING – Compare items as strings3 = SORT_LOCALE_STRING – Compare items as strings, based on current locale4 = SORT_NATURAL – Compare items as strings using natural ordering5 = SORT_FLAG_CASE –

Syntax

asort(array,sortingtype);

Example

$name_age=array("Cooper"=>"40","Samuel"=>"37","Alex"=>"43");
asort($name_age);
print_r($name_age);

Output

Array ( [Samuel] => 37 [Cooper] => 40 [Alex] => 43 ) 

Leave a Reply

Your email address will not be published. Required fields are marked *