Current Location: Home> Function Categories> krsort

krsort

Reversely sort the array by key name
Name:krsort
Category:Array
Programming Language:php
One-line Description:Sorting the array in reverse by key name.

Definition and usage

krsort() function sorts the associative arrays in descending order by key names.

Tip: Please use the ksort() function to sort the associative arrays according to the key names.

Tip: Please use the arsort() function to sort the associative arrays in descending order by key values.

Example

Sort the associative arrays in descending order by key names:

 <?php
$age = array ( "Bill" => "60" , "Steve" => "56" , "mark" => "31" ) ;
krsort ( $age ) ;
?>

Try it yourself

grammar

 krsort ( array , sortingtype ) ;
parameter describe
array Required. Specifies the array to be sorted.
sortingtype

Optional. Specifies how to arrange elements/items of an array. Possible values:

  • 0 = SORT_REGULAR - Default. Arrange each item in a regular order (Standard ASCII, without changing the type)
  • 1 = SORT_NUMERIC - Treat each item as a number.
  • 2 = SORT_STRING - Handle each item as a string.
  • 3 = SORT_LOCALE_STRING - Handle each item as a string, based on the current locale setting (can be changed via setlocale()).
  • 4 = SORT_NATURAL - Handle each item as a string, using a natural sort like natsort().
  • 5 = SORT_FLAG_CASE - Strings can be sorted in combination with (bit-by-bit or) SORT_STRING or SORT_NATURAL, case-insensitive.

illustrate

The krsort() function reverses the array by keys, retaining the original key for the array value.

The optional second parameter contains the additional sort flag.

If successful, return TRUE, otherwise return FALSE.