PHP krsort()Functions

PHP Array ReferenceComplete PHP Array Reference

Examples

Sort an associative array in descending order by key:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
krsort($age);
?>

Run example »

Definition and Usage

The krsort() function sorts an associative array in descending order by key.

Tip:Use theksort()function to sort an associative array in ascending order by key.

Tip:Use thearsort()function to sort an associative array in descending order by value.

Syntax

krsort(array,sortingtype);

Parameter Description
array Required. Specifies the array to be sorted.
sortingtype Optional. Specifies how to arrange the elements/items of the array. Possible values:
  • 0 = SORT_REGULAR - Default. Arrange each item in regular order (Standard ASCII, without changing types).
  • 1 = SORT_NUMERIC - Treat each item as a number.
  • 2 = SORT_STRING - Treat each item as a string.
  • 3 = SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed viasetlocale()).
  • 4 = SORT_NATURAL - Treat each item as a string, using natural ordering similar tonatsort()natural sort.
  • 5 = SORT_FLAG_CASE - Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.

Technical Details

Return value: Returns TRUE on success, or FALSE on failure.
PHP Version: 4+


PHP Array ReferenceComplete PHP Array Reference Other Extensions