PHP rsort() Function
In this chapter you will learn:
- Definition for PHP rsort() Function
- Syntax for PHP rsort() Function
- Parameter for PHP rsort() Function
- Note for PHP rsort() Function
- Example - PHP rsort() Function
- Example - Sort the elements of the $numbers array in descending numerical order
- Example - Compare the items numerically and sort the elements of the $cars array in descending order
Definition
The rsort() function sorts an indexed array in descending order.
Syntax
PHP rsort() Function has the following syntax.
rsort(array,sortingtype);
Parameter
Parameter | Is Required | Description |
---|---|---|
array | Required. | Array to sort |
sortingtype | Optional. | How to compare the array elements/items. |
Possible values for sortingtype:
Value | Description |
---|---|
0 = SORT_REGULAR | Default. Compare items normally (don't change types) |
1 = SORT_NUMERIC | Compare items numerically |
2 = SORT_STRING | Compare items as strings |
3 = SORT_LOCALE_STRING | Compare items as strings, based on current locale |
4 = SORT_NATURAL | Compare items as strings using natural ordering |
5 = SORT_FLAG_CASE | an be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively |
Note
The values in the sorted arrays have different keys from the values in the original array.
The sort() and rsort() functions reindex the original array.
Example 1
<?php// j a v a 2s. c o m
$cars=array("A","B","C");
rsort($cars);
$authors = array( "Java", "PHP", "CSS", "HTML" );
rsort( $authors );
print_r( $authors );
?>
The code above generates the following result.
Example 2
Sort the elements of the $numbers array in descending numerical order:
<?php// j a v a2 s .co m
$numbers=array(4,6,2,22,11);
rsort($numbers);
print_r($numbers);
?>
The code above generates the following result.
Example 3
Compare the items numerically and sort the elements of the $cars array in descending order:
<?php/* java 2s. c o m*/
$cars=array("A","B","C","1","10");
rsort($cars,SORT_NUMERIC);
print_r($cars);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP shuffle() Function
- Syntax for PHP shuffle() Function
- Parameter for PHP shuffle() Function
- Return for PHP shuffle() Function
- Example for PHP shuffle() Function
- Example - Randomize the order of the elements in the array
- Example - Randomize the order of the elements in the associate array
Home » PHP Tutorial » PHP Array Functions