PHP | krsort() Function

The krsort() function is an inbuilt function in PHP which is used to sort an array by key in reverse order according to its index values. It sorts in a way that relation between indices and values is maintained.

Syntax:

bool krsort( $array, $sorting_type )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $array: This parameter specifies the array which to be sort. It is a mandatory parameter.
  • $sorting_type: This is an optional parameter. There are different sorting types which are discussed below:
    • SORT_REGULAR: The value of $sorting_type is SORT_REGULAR then items are compare normally.
    • SORT_NUMERIC: The value of $sorting_type is SORT_NUMERIC then items are compares numerically.
    • SORT_STRING: The value of $sorting_type is SORT_STRING then items are compares as string.
    • SORT_LOCALE_STRING: The value of $sorting_type is SORT_STRING then items are compares as string, based on current locale.

Return Value: This function returns True on success or False on failure.

Below programs illustrate the krsort() function in PHP.
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate
// krsort()function
  
// Input different array elements
$arr = array("0" =>"Technology",
             "1" =>"Machine",
             "2" =>"GeeksforGeeks",
             "3" =>"Graphics",
             "4" =>"Videos",
             "5" =>"Report",
             "6" =>"Article",
             "7" =>"Placement",
             "8" =>"Contribute",
             "9" =>"Reset",
             "10" =>"Copy",
        );
  
// Implementation of krsort()
krsort($arr);
  
// for-Loop for displaying result
foreach ($arr as $key => $val) {
    echo "[$key] = $val";
    echo"\n";
}
  
?>

chevron_right


Output:

[10] = Copy
[9] = Reset
[8] = Contribute
[7] = Placement
[6] = Article
[5] = Report
[4] = Videos
[3] = Graphics
[2] = GeeksforGeeks
[1] = Machine
[0] = Technology

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate
// krsort function
      
// Input different array elements
$arr = array("a" => 11,
             "b" => 22,
             "d" => 33,
             "n" => 44,
             "o" => 55,
             "p" => 66,
             "r" => 77,
             "s" => 2,
             "q" => -11,
             "t" => 3,
             "u" => 1000,
             "z" => 1,                            
        );
// Implementation of krsort
krsort($arr);
  
// for-Loop for displaying result
foreach ($arr as $key => $val) {
    echo "[$key] = $val";
    echo"\n";
}
  
?>

chevron_right


Output:

[z] = 1
[u] = 1000
[t] = 3
[s] = 2
[r] = 77
[q] = -11
[p] = 66
[o] = 55
[n] = 44
[d] = 33
[b] = 22
[a] = 11

Related Articles:

Reference: http://php.net/manual/en/function.krsort.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2

Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.