PHP | uasort() Function

The uasort() function is a builtin function in PHP and is used to sort an array such that array indices maintain their correlation with the array elements they are associated with, using a user-defined comparison function.

Syntax:

boolean uasort(array_name, user_defined_function);

Parameter: This function accepts two parameters and are described below:



  1. array_name: This parameter represents the array which we need to sort.
  2. user_defined_function: This is a comparator function and is used to compare values and sort the array. This function returns three types of values described below:
    • It return 0 when a=b
    • It return 1 when a>b and we want to sort input array in ascending order otherwise it will return -1 if we want to sort input array in descending order.
    • It return -1 when a<b and we want to sort input array in ascending order otherwise it will return 1 if we want to sort input array in descending order.

Return Value: It returns a boolean value, i.e. either TRUE on success and FALSE on failure.

Examples:

Input: array
       (
            "a" => 4,
            "b" => 2,
            "g" => 8,
            "d" => 6,
            "e" => 1,
            "f" => 9
       )
Output: Array 
        ( 
            [e] => 1 
            [b] => 2 
            [a] => 4 
            [d] => 6 
            [g] => 8 
            [f] => 9 
        )

Below programs illustrates the uasort() function in PHP:

  • Sorting in ascending order: To sort the input array in ascending order, in the comparator function we will return 1 when a>b or -1 when a<b. Below program illustrates this:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    // PHP program to sort in ascending
    // order using uasort() function
      
    // user_defined comparator function
    function sorting($a,$b)
    {
        if ($a==$b) return 0;
            return ($a<$b)?-1:1;
    }
      
    // input array  
    $arr=array("a"=>4,"b"=>2,"g"=>8,"d"=>6,"e"=>1,"f"=>9);
      
    uasort($arr,"sorting");
      
    // printing sorted array.
    print_r($arr);
      
    ?>

    chevron_right

    
    

    Output:

    Array
    (
        [e] => 1
        [b] => 2
        [a] => 4
        [d] => 6
        [g] => 8
        [f] => 9
    )
    
  • Sorting in descending order: To sort the input array in descending order, in the comparator function we will return -1 when a>b or 1 when a<b. Below program illustrates this:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    // PHP program to sort in descending
    // order using uasort() function
      
    // user_defined comparator function
    function sorting($a, $b
    {
        if ($a == $b) return 0;
            return ($a > $b) ? -1 : 1;
    }
      
    // input array
    $input = array("d"=>"R", "a"=>"G", "b"=>"X", "f"=>"Z" );
      
    uasort($input, "sorting");
      
    // printing sorted array.
    print_r($input);
      
    ?>

    chevron_right

    
    

    Output:

    Array
    (
        [f] => Z
        [b] => X
        [d] => R
        [a] => G
    )
    

Reference:
http://php.net/manual/en/function.uasort.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.