PHP arsort() function with example

PHP arsort() function with example

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

PHP arsort() work

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

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

Syntax:

    arsort(array, [mode]);

Here,

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

Example:

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

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

PHP Code:

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

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

Output:

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

Recent Posts

error: Alert: Content is protected!!