PHP asort() function with example

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

PHP asort() work

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

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

Syntax:

rsort(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 think about things regularly
    • 1 – It is utilized to look at things numerically
    • 2 – It is utilized to look at things as strings
    • 3 – It is utilized to look at things as present district strings
    • 4 – It is utilized to look at things as strings (common requesting)

Example:

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

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

PHP Code:

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

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

Output:

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

Leave a Comment

error: Alert: Content is protected!!