PHP | ArrayIterator uasort() Function Last Updated : 21 Nov, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The ArrayIterator::uasort() function is an inbuilt function in PHP which is used to sort the element using a user-defined comparison function and maintain their index association. Syntax: void ArrayIterator::uasort( callable $cmp_function ) Parameters: This function accepts a single parameter $cmp_function which is the user-defined comparison function. This comparison function accepts two parameters which are the values of the ArrayIterator 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 return any value. Below programs illustrate the ArrayIterator::uasort() function in PHP: strong>Program 1: php <?php // Declare an ArrayIterator $arrItr = new ArrayIterator( array( "a" => 4, "b" => 2, "g" => 8, "d" => 6, "e" => 1, "f" => 9 ) ); // User defined comparator function function sorting($a, $b) { if($a == $b) return 0; return ($a < $b) ? -1 : 1; } $arrItr->uasort("sorting"); // Printing the sorted array. print_r($arrItr); ?> Output: ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [e] => 1 [b] => 2 [a] => 4 [d] => 6 [g] => 8 [f] => 9 ) ) Program 2: php <?php // Declare an ArrayIterator $arrItr = new ArrayIterator( array( "a" => "Geeks", "b" => "for", "c" => "Geeks", "d" => "Computer", "e" => "Science", "f" => "Portal" ) ); // Declare a comparison function to sort // values in descending order function comparison($val1, $val2) { if ($val1 == $val2) { return 0; } else if($val1 > $val2) return -1; else return 1; } $arrItr->uasort('comparison'); // Print the sorted ArrayObject print_r($arrItr); ?> Output: ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [b] => for [e] => Science [f] => Portal [a] => Geeks [c][/c] => Geeks [d] => Computer ) ) Reference: https://www.php.net/manual/en/arrayiterator.uasort.php Comment More infoAdvertise with us Next Article PHP | ArrayIterator uasort() Function J jit_t Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-Iterators PHP-ArrayIterator +1 More Similar Reads PHP | ArrayIterator uksort() Function The ArrayIterator::uksort() function is an inbuilt function in PHP which is used to sort the keys by using a user-defined comparison function. Syntax: void ArrayIterator::uksort( callable $cmp_function ) Parameters: This function accepts single parameter $cmp_function which holds the user defined co 2 min read PHP | ArrayIterator seek() Function The ArrayIterator::seek() function is an inbuilt function in PHP which is used to seek the position of an array iterator. Syntax: void ArrayIterator::seek( int $position ) Parameters: This function accepts single parameter $position which holds the position to seek. Return Value: This function does 1 min read PHP | ArrayIterator seek() Function The ArrayIterator::seek() function is an inbuilt function in PHP which is used to seek the position of an array iterator. Syntax: void ArrayIterator::seek( int $position ) Parameters: This function accepts single parameter $position which holds the position to seek. Return Value: This function does 1 min read PHP | ArrayIterator valid() Function The ArrayIterator::valid() function is an inbuilt function in PHP which is used to check whether an array contains more entries or not. Syntax: bool ArrayIterator::valid( void ) Parameters: This function does not accept any parameters. Return Value: This function returns TRUE if the iterator is vali 1 min read PHP | ArrayIterator rewind() Function The ArrayIterator::rewind() function is an inbuilt function in PHP which is used to rewind the array back to the start. Syntax: void ArrayIterator::rewind( void ) Parameters: This function does not accept any parameters. Return Value: This function does not return any value. Below programs illustrat 1 min read Like