PHP Array Elements Sorting Function

What is arsort() Function?

Explanation

The "arsort()" function sorts an array elements by the values in reverse order, but the keys remain the same.

Syntax:


arsort(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"=>"Cherry","2"=>"Apple");
arsort($b);
print_r($b);
?>
Result :

Array ( [1] => Cherry [2] => Apple )

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

PHP Topics


Ask Questions

Ask Question