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
)