|
|
PHP Array Elements Sorting Function
|
Tutorials » Php »
|
Topic |
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.
|
|
A Note |
Learn PHP programming language tutorial with simple and neat example. Hope you enjoy this free tutorial.
Do give us your valuable feedback and suggestions on this online tutorial. This is a Copyright Content.
|
|
|
|