PHP krsort() Function

PHP krsort() Function

The krsort() function in PHP sorts an array by key in reverse order while maintaining the keys.

Syntax

krsort (array & $array[,int $sort_flags= SORT_REGULAR] )

Parameter

array(required)- This parameter represents the input array.

sort_flags(optional)- This parameter is used to modify the behavior of the sort. The various sorting type flags are as follows:

  • SORT_REGULAR (default) – It is used to compare items normally without changing types.
  • SORT_NUMERIC – This flag compares items numerically.
  • SORT_STRING – It compares the items as strings.
  • SORT_LOCALE_STRING – This flag compares the given items as strings, based on the current locale.
  • SORT_NATURAL – It is used to compare the items as strings using "natural ordering" .
  • SORT_FLAG_CASE – This flag can be combined with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.

Return

This parameter returns a Boolean value TRUE on success or FALSE on failure.

Example 1

"Reema", "a"=>"Varun", "b"=>"Mishra", "c"=>"Radhika");
echo("Initialized Array: ");
print_r($array);
//soring the keys if the array in reverse order
krsort($array);
echo("\nSorted Array: \n");
print_r($array);//will print order unlike d,c,b,a
?> 

Output

Initialized Array: Array
(
    [d] => Reema
    [a] => Varun
    [b] => Mishra
    [c] => Radhika
)
Sorted Array: 
Array
(
    [d] => Reema
    [c] => Radhika
    [b] => Mishra
    [a] => Varun
) 

Example 2

"Reema", "Age"=>"23","Country"=>"India");
//sorting the keys of the array in reverse order
krsort($array);
//printing the reversed array
echo("\nReversed key array: \n");
foreach ($array as $key => $val) //the order will be Name, Country, Age
{
    echo "$key = $val\n";
}
?> 

Output

Reversed key array: 
Name = Reema
Country = India
Age = 23 

Example 3

 $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}
//Sorting the keys of the array in reverse order
krsort($fruits);
echo("\n\nAfter sorting the array in reverse order...\n Reversed Array: \n");
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}
?> 

Output

Original Array: 
fruits[0] = lemon
fruits[1] = orange
fruits[2] = banana
fruits[3] = apple
After sorting the array in reverse order...
 Reversed Array: 
fruits[3] = apple
fruits[2] = banana
fruits[1] = orange
fruits[0] = lemon 

Example 4

 array(1 => "mango", 2 => "banana", 2 => "apple"),
    "numbers" => array(1, 2, 3, 4, 5, 6),
    "values"   => array("first", 5 => "second", "third")
);
//sorting the keys of the array in reverse order
krsort($array);
//printing the reversed array
print_r($array);
?> 

Output

Array
(
    [values] => Array
        (
            [0] => first
            [5] => second
            [6] => third
        )
    [numbers] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
        )
    [fruits] => Array
        (
            [1] => mango
            [2] => apple
        )
) 

Example 5

"Reema", "Age"=>23,"Country"=>"India", 1=>"I Love India");
//original array
echo("Original array: \n");
print_r($array);
//sorting the keys of the array
krsort($array );
//printing the sorted array
echo("\nSorted array without second parameter: \n");
print_r($array);
//sorting the keys of array with second parameter as SORT_STRING 
krsort($array,SORT_STRING  );
//printing the sorted key array
echo("\nSorted array with second parameter: \n");
print_r($array);
?> 

Output

Original array: 
Array
(
    [Name] => Reema
    [Age] => 23
    [Country] => India
    [1] => I Love India
)
Sorted array without second parameter: 
Array
(
    [1] => I Love India
    [Name] => Reema
    [Country] => India
    [Age] => 23
)
Sorted array with second parameter: 
Array
(
    [Name] => Reema
    [Country] => India
    [Age] => 23
    [1] => I Love India
) 

Related Topics

PHP list() Function

PHP list() Function The list() function in PHP sorts an array by key while maintaining the keys. Syntax list ( mixed $var1 [, mixed $... ] ) Parameter var1(required)- This parameter accepts variables separated by spaces. …(optional)- This parameter accepts a list of variables separated...

1 minute read.

PHP array_diff_ukey() Function

The array_diff_ukey() function in PHP compares the keys from array1 against the keys from array2 and returns the difference containing all the entries from array1 that are not present in any of the other arrays. Syntax array_diff_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func ) Parameter array1(required)- This parameter signifies the...

1 minute read.

PHP Example

We can create a simple program by writing the source code inside the php tag and save it with .php extension. Syntax: <?php // your code here ?> Let us take an example of PHP. <!DOCTYPE html> <html> <head>            ...

1 minute read.

PHP array_column() Function

PHP array_column() Function The array_column() function in PHP is used to return the values from a single column specified in the input array. Syntax array_column (array $input, mixed $column_key [, mixed $index_key]) Parameter input(required)- This parameter represents a multi-dimensional array or an array of...

1 minute read.

PHP htmlspecialchars_decode() Function

PHP htmlspecialchars_decode() Function The htmlspecialchars_decode() function in PHP convert some predefined HTML entities to characters. The some predefined HTML entities that will be decoded are as follows: &amp; OR & (ampersand)&quot; OR " (double quote)&#039;...

2 minutes read.

PHP array_merge() Function

PHP array_merge() Function The array_merger() Function in PHP merges the elements of one or more arrays wherein the values of one are appended to the end of the previous one. It returns the resulting array...

1 minute read.

PHP parse_str() Function

PHP parse_str() Function The parse_str() function in PHP parses a query string into variables. Syntax parse_str ( string $encoded_string [, array &$result ] ) Parameter encoded_string(required)- This parameter represents the input string. result(optional)- This parameter specifies the name of an...

1 minute read.

PHP Include Function

PHP Include( ) The PHP include() statement holds all the code/text that liesin the defined file and paste it into the file that uses the "include" statement. With the help of include...

3 minutes read.

PHP addcslashes() Function

The addcslashed() function in PHP is used to add quote string with slashes in a C style. It is case-sensitive function.  Syntax addcslashes (str, charlist ) Parameter str(required)- This parameter represents the string to be escaped. charlist(required)-...

1 minute read.

PHP array() function

PHP array() function The array() function in PHP is used to create an array. Syntax array ([ mixed $... ] ) Parameter …(optional)- This parameter represents the elements’ values along with keys of the array. The syntax of this is as follows: "index => values"...

1 minute read.

PHP strip_tags() Function

PHP strip_tags() Function The strip_tags() function in PHP is sued to strip a string from HTML, XML, and PHP tags. It is a binary-safe function. Syntax strip_tags ( string $str [, string $allowable_tags ] ) Parameter str(required)- This parameter specifies the input string. allowable_tags(optional)- This parameter represents...

1 minute read.

PHP sha1_file() Function

PHP sha1_file() Function The sha1_file() in PHP calculates the sha1 hash of a file and returns the hash (40-character hexadecimal number). Syntax sha1_file ( string $filename [, bool $raw_output] )  Parameter filename(required)- This parameter represents the filename of the file to hash. raw_output(optional)- This...

1 minute read.

PHP similar_text() Function

PHP similar_text() Function The similar_text() function in PHP calculates the similarity between two strings. Syntax similar_text ( string $first , string $second [, float &$percent ] ) Parameter first(required): This parameter represents the first string to be compared. second(required): This parameter represents the second string to be compared. percent(optional): This...

1 minute read.

PHP count() function

PHP count() function The count() function in PHP is used to count all elements in the specified array or something in an object. Syntax count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )   Parameter array_or_countable(required)– This parameter represents the input array or countable object. mode(optional)- This...

2 minutes read.

PHP array_splice() Function

PHP array_splice() Function The array_splice() function in PHP is used to remove a portion of the array and replaces them with the elements of the replacement array. Syntax array_splice (array &$input, int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) Parameter input(required)- This array represents the...

2 minutes read.

How to use Date and Time in PHP

HOW TO USE DATE AND TIME IN PHP: PHP time and date function  informs us about current date and time. it can be manipulated also. How time() function works: The time() function...

2 minutes read.

PHP strtoupper() Function

The strtoupper() function in PHP is used to convert a string to uppercase. This function is binary-safe. The related functions are as follows: strtolower()lcfirst()ucfirst()ucwords() Syntax strtoupper ( string $string ) Parameter string(required)- This parameter represents the input string. Return This function returns a string with all alphabetic...

1 minute read.

PHP Continue

What is continue in PHP? Inside the loop, the continue statement is used to control the loop in preprocessor hypertext. PHP utilizes the continue keyword to implement the continue statement, which...

3 minutes read.

PHP array_udiff() Function

PHP array_udiff() Function The array_udiff() function in PHP calculates the difference of arrays by using a callback function for data comparison. Syntax array_udiff ( array $array1 , array $array2 [, array $... ], callable $value_compare_func ) Parameter array1(required)- This array represents the first input array. array2(required)- This array represents the second input array. array3….(optional)- It...

1 minute read.

PHP nl_langinfo() Function

PHP nl_langinfo() Function The nl_langinfo() function in PHP returns specific local information. This function does not work on Windows platforms. Syntax nl_langinfo ( int $item ) Parameter Item(required)- This parameter specifies the  integer value of the element or the constant name of the...

1 minute read.