PHP krsort() function with example

PHP krsort() work: Here, we will find out about the krsort() work with the example in PHP?

PHP krsort() work

krsort() work is utilized to sort an affiliated exhibit in plummeting request dependent on the keys, as we realize that a cooperative cluster contains keys and values, this technique sorts a cluster as per the keys.

It doesn’t restore an arranged exhibit, it sorts the information cluster.

Syntax:

krsort(array, [mode]); 

Here,

  • the exhibit is an information cluster
  • the mode is a discretionary parameter, its default value is 0, it has the following values:
    • 0 – It is utilized to analyze things ordinarily
    • 1 – It is utilized to look at things numerically
    • 2 – It is utilized to think about things as strings
    • 3 – It is utilized to analyze things as present area strings
    • 4 – It is utilized to look at things as strings (regular requesting)

Example:d

    Input:
    $person = array(
        "kishan" => 21,
        "rajesh" => 21,
        "durgesh" => 20,
        "aakash" => 27,
        "manish" => 25
        );

    Output:
    sorted array...  
    Array
    (    
        [kishan] => 21
        [aakash] => 27 
        [manish] => 25
        [rajesh] => 21 
        [durgesh] => 20 
    ) 

PHP Code:

<?php    
    $person = array(
        "kishan" => 21,
        "rajesh" => 21,
        "durgesh" => 20,
        "aakash" => 27,
        "manish" => 25
        );

    print ("unsorted array...\n");
    print_r ($person);
    //sorting...
    
    krsort($person);
    print ("sorted array...\n");
    print_r ($person);    
?>

Output:

unsorted array...
Array
(    
    [kishan] => 21
    [rajesh] => 21 
    [durgesh] => 20 
    [aakash] => 27 
    [manish] => 25
)    
sorted array...  
Array
(    
    [kishan] => 21
    [aakash] => 27 
    [manish] => 25
    [rajesh] => 21 
    [durgesh] => 20 
)

Leave a Comment

error: Alert: Content is protected!!