ArrayObject uksort() Function in PHP

The uksort() function of the ArrayObject class in PHP is used to sort the entries present in the ArrayObject according to the keys following a user-defined function. They key-value mapping is preserved after sorting the ArrayObject.

Syntax:

void uksort($comparator) 

Parameters: This function accepts a single parameter $comparator which is the user defined comparison function. This comparison function in turn accepts two arguments which are the keys of the ArrayObject and returns less than, equals to or greater than zero if the first argument is less than, equals to or greater than zero respectively.



Return Value: This function does not returns any value.

Below program illustrate the above function:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate the
// uksort() function
  
$arr = array("1"=>"Welcome", "2" => "to", "3" => "GfG");
  
// Create array object
$arrObject = new ArrayObject($arr);
  
// Declare a comparison function to sort 
// the entries by keys in descending order
function comparison($val1, $val2) {
    if ($val1 == $val2) {
        return 0;
    }
    else if($val1 > $val2)
        return -1;
    else
        return 1;
}
  
$arrObject->uksort('comparison');
  
// Print the sorted ArrayObject
print_r($arrObject);
  
?>

chevron_right


Output:

ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [3] => GfG
            [2] => to
            [1] => Welcome
        )

)

Reference: http://php.net/manual/en/arrayobject.uksort.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.


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.