PHP ksort() function with example

PHP ksort() function with example

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

PHP ksort() work

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

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

Syntax:

    ksort(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 analyze things numerically
    • 2 – It is utilized to analyze things as strings
    • 3 – It is utilized to analyze things as present district strings
    • 4 – It is utilized to think about things as strings (regular requesting)

Example:

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

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

PHP Code:

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

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

Output:

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

Recent Posts

error: Alert: Content is protected!!