PHP rsort() Function

In this chapter you will learn:

  1. Definition for PHP rsort() Function
  2. Syntax for PHP rsort() Function
  3. Parameter for PHP rsort() Function
  4. Note for PHP rsort() Function
  5. Example - PHP rsort() Function
  6. Example - Sort the elements of the $numbers array in descending numerical order
  7. 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

ParameterIs RequiredDescription
arrayRequired.Array to sort
sortingtypeOptional.How to compare the array elements/items.

Possible values for sortingtype:

ValueDescription
0 = SORT_REGULARDefault. Compare items normally (don't change types)
1 = SORT_NUMERICCompare items numerically
2 = SORT_STRINGCompare items as strings
3 = SORT_LOCALE_STRINGCompare items as strings, based on current locale
4 = SORT_NATURALCompare items as strings using natural ordering
5 = SORT_FLAG_CASEan 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:

  1. Definition for PHP shuffle() Function
  2. Syntax for PHP shuffle() Function
  3. Parameter for PHP shuffle() Function
  4. Return for PHP shuffle() Function
  5. Example for PHP shuffle() Function
  6. Example - Randomize the order of the elements in the array
  7. Example - Randomize the order of the elements in the associate array
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function