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 comparison function.
Return Value: This function does not return any value.
Below programs illustrate the ArrayIterator::uksort() function in PHP:
Program 1:
<?php
$arrItr = new ArrayIterator(
array (
"a" => 4,
"b" => 2,
"g" => 8,
"d" => 6,
"e" => 1,
"f" => 9
)
);
function sorting( $a , $b ) {
if ( $a == $b )
return 0;
return ( $a < $b ) ? -1 : 1;
}
$arrItr ->uksort( "sorting" );
print_r( $arrItr );
?>
|
Output:
ArrayIterator Object
(
[storage:ArrayIterator:private] => Array
(
[a] => 4
[b] => 2
[d] => 6
[e] => 1
[f] => 9
[g] => 8
)
)
Program 2:
<?php
$arrItr = new ArrayIterator(
array (
"b" => "for" ,
"a" => "Geeks" ,
"e" => "Science" ,
"c" => "Geeks" ,
"f" => "Portal" ,
"d" => "Computer"
)
);
function comparison( $val1 , $val2 ) {
if ( $val1 == $val2 ) {
return 0;
}
else if ( $val1 > $val2 )
return -1;
else
return 1;
}
$arrItr ->uksort( 'comparison' );
print_r( $arrItr );
?>
|
Output:
ArrayIterator Object
(
[storage:ArrayIterator:private] => Array
(
[f] => Portal
[e] => Science
[d] => Computer
=> Geeks
[b] => for
[a] => Geeks
)
)
Reference: https://www.php.net/manual/en/arrayiterator.uksort.php
Last Updated :
21 Nov, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...