PHP | ArrayIterator uksort()函数
ArrayIterator::uksort()函数是PHP中的一个内置函数,用于使用用户定义的比较函数对键进行排序。
句法:
void ArrayIterator::uksort( callable $cmp_function )
参数:此函数接受单个参数$cmp_function ,其中包含用户定义的比较函数。
返回值:此函数不返回任何值。
下面的程序说明了PHP中的 ArrayIterator::uksort()函数:
方案一:
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->uksort("sorting");
// Printing the sorted array.
print_r($arrItr);
?>
输出:
ArrayIterator Object
(
[storage:ArrayIterator:private] => Array
(
[a] => 4
[b] => 2
[d] => 6
[e] => 1
[f] => 9
[g] => 8
)
)
方案二:
"for",
"a" => "Geeks",
"e" => "Science",
"c" => "Geeks",
"f" => "Portal",
"d" => "Computer"
)
);
// 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->uksort('comparison');
// Print the sorted ArrayObject
print_r($arrItr);
?>
输出:
ArrayIterator Object
(
[storage:ArrayIterator:private] => Array
(
[f] => Portal
[e] => Science
[d] => Computer
=> Geeks
[b] => for
[a] => Geeks
)
)
参考: https://www. PHP.net/manual/en/arrayiterator.uksort。 PHP