krsort() function in PHP


The krsort() method sorts an array by key in reverse order. It returns TRUE on success or FALSE on failure.

Syntax

ksort(arr, flag)

Parameters

  • arr − The array to sort.

  • flag

    • 0 = SORT_REGULAR - Default. Compare items normally. Do not 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.

Return

The krsort() function returns TRUE on success or FALSE on failure.

Example

The following is an example −

 Live Demo

<?php
$product = array("p"=>"headphone", "r"=>"mouse", "q"=>"pendrive");
krsort($product);
foreach ($product as $key => $val) {
   echo "$key = $val
"; } ?>

Output

The following is the output −

r = mouse
q = pendrive
p = headphone

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 24-Jun-2020

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements