English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP krsort() Function Usage and Example

PHP Array Function Manual

The krsort() function sorts the array in reverse order by key name

Syntax

krsort ( $array, $sort_flag );

Definition and Usage

The krsort() function sorts the array in reverse order by key. These values retain their original keys.

Parameter

Serial NumberParameters and Description
1

array(Required)

It specifies an array

2

sort_flag(Optional)

It specifies how to sort the array values. Possible values-

  • SORT_REGULAR - Default Value. Treat the value as is (do not change the type)

  • SORT_NUMERIC - Handle the value numerically

  • SORT_STRING - Treat the value as a string

  • SORT_LOCALE_STRING - Treat the value as a string based on local settings

Return Value

This function returns TRUE on success and FALSE on failure.

Online Example

<?php
   $transport = array( 'a'=>'foot', 'b'=>'bike', 'c'=>'car', 'd'=>'plane');
   krsort($transport);
   print_r($transport);
?>
Test and see‹/›

Output Result:

Array ( [d] => plane [c] => car [b] => bike [a] => foot )

  PHP Array Function Manual