PHP Array Elements Sorting Function

What is asort() Function?

Explanation

The "asort()" function used to sorting an array elements by the values and keep the original keys.

Syntax:


asort(array,sorttype)

In the above syntax "array" is the array to be sorted, "sorttype" specifies the method to be used for sorting, it can have values "SORT_REGULAR" for default, "SORT_NUMERIC" treat values numerically, "SORT_STRING" treat values as strings,"SORT_LOCALE_STRING" treat values as strings based on local settings .

Example :


<?php
$b=array("1"=>"Grapes","2"=>"Apple","3" =>"Cherry");
asort($b);
print_r($b);
?>
Result :

Array
{
[2] => Apple
[3] => Cherry
[1] => Grapes
}

In the above example all the elements of the array "$b" is sorted using the default type "SORT_REGULAR" based on the values and the keys remain the same.

PHP Topics


Ask Questions

Ask Question