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 key() Function

PHP key() Function The key() function in PHP fetches a key from an array and returns the index element of the current array position. Syntax key ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the key of the...

1 minute read.

PHP pos() Function

PHP pos() Function The pos() function in PHP returns the current element in an array which is initialized to the first element of the array. This function is an alias of current(). Syntax pos ( array...

1 minute read.

PHP array_map() Function

PHP array_map() Function The array_map() function  in PHP is used to return an array containing the results of applying the user-defined callback function over the elements of one or more arrays. Syntax array_map ( callable $callback , array $array1 [, array $... ] )  Parameter callable(required)- This parameter...

1 minute read.

PHP array_shift() Function

PHP array_shift() Function The array_shift() in PHP shifts the first value of the array off. This function returns the same value after shortening the array by one element and moving everything. Syntax array_shift ( array &$array )  Parameter array(required)- This parameter represents the input array Return This function returns...

1 minute read.

PHP array_walk() Function

PHP array_walk() Function The array_walk() function in PHP is used to apply the user-defined callback function to each element of the array. This function returns a Boolean TRUE on success or FALSE on failure. Syntax array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] ) Parameter array(required)- This array represents...

1 minute read.

PHP MySQL Database Tutorial

MySQL is the most popular database system that is used within PHP. Let's first understand about MySQL. What is MySQL? MySQL is open-source Relational Database Management System (RDMS). The name of MySQL is...

13 minutes read.

PHP natcasesort() Function

PHP natcasesort() Function The natcasesort() function in PHP Sort an array using a case insensitive "natural order" algorithm while maintaining the keys. Syntax natcasesort ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns a Boolean...

1 minute read.

PHP htmlentities() Function

PHP htmlentities() Function The htmlentities() function in PHP converts all characters to HTML entities. Syntax htmlentities(string,flags,character-set,double_encode);   Parameter string(required)- This parameter specifies the string to convert. flags(optional)- This parameter specifies how to handle quotes, invalid encoding and the used document type. The...

1 minute read.

PHP String

PHP string is a sequence of characters. It is used to store and manipulate text. We can specify a string literal in four ways: single quoted double quoted heredoc syntax ...

2 minutes read.

PHP array_values() Function

PHP array_values() Function The array_values () function in PHP returns all the values from the array and indexes the array numerically. Syntax array_values ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns an indexed array of values. Example 1 Output Input Array: Array ...

1 minute read.

PHP ksort() Function

PHP ksort() Function The ksort() function in PHP sorts an array by key while maintaining the keys. Syntax ksort(array & $array[,int $sort_flags = SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This parameter is...

2 minutes read.

PHP str_word_count() Function

PHP str_word_count() Function The str_word_count() function in PHP counts the number of words inside the string and returns the information about words used in a string. String str_word_count( string $string [, int $format [, string $charlist ]] ) Parameter string(required)- This parameter represents the input string. format(required)- This parameterspecifies the...

2 minutes read.

PHP str_ireplace() Function

PHP str_ireplace() Function The PHP str_ireplace() function in PHP replaces some characters with some other characters in a string. Syntax str_ireplace ( mixed $search , mixed  $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter specifies the value being searched for. replace(required)- This parameter represents the replacement value...

1 minute read.

PHP key_exists() Function

PHP key_exists() Function The key_exists() function in PHP checks if the specified key or index exists in the array or not. Syntax key_exists ( mixed $key , array $array )  Parameter key(required)- This parameter represents the value to check. array(required)- This parameter signifies an input array with...

1 minute read.

PHP array_intersect_assoc() function

PHP array_intersect_assoc() function The array_intersect_assoc() function in PHP is used to compute the intersection of arrays with the help of an additional index check. It returns an array containing all the entries from array1 that are present...

1 minute read.

PHP unserialize() Function

PHP unserialize() Function The unserialize() function in PHP takes a single serialized variable and converts it back into a PHP value. Syntax unserialize ( string $str [, array $options ] ) Parameter value(required)- This parameter represents the serialized string. options(optional)- This parameter signifies any options to be...

1 minute 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 strrrev() Function

PHP strrrev () Function The strrev() function in PHP reverses a string. Syntax strrev ( string $string ) Parameter string(required)- This parameter represents the input string to be reversed. Return This function returns the reversed string. Example 1 Output Initial string: Hello...

1 minute read.

PHP $ and $$ Variables

PHP $var variable is a normal variable that stores any value. PHP $$var variable is a reference variable that stores the value of$var inside it. Let us take an example. Example 1 <?php $x =...

1 minute read.

PHP Variables

PHP Variable is the name of memory location used to hold the data.  In PHP, variable starts with $ symbol. Rules for PHP Variables. Variable starts with $ sign. Variable name...

2 minutes read.