ArrayIterator::uksort()函数是PHP中的内置函数,用于通过使用用户定义的比较函数对键进行排序。
用法:
void ArrayIterator::uksort( callable $cmp_function )
参数:该函数接受单个参数$cmp_function,该参数保存用户定义的比较函数。
返回值:该函数不返回任何值。
以下示例程序旨在说明PHP中的ArrayIterator::uksort()函数:
程序1:
<?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->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 ) )
程序2:
<?php
// Declare an ArrayIterator
$arrItr = new ArrayIterator(
array(
"b" => "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
相关用法
- PHP uksort()用法及代码示例
- PHP ArrayObject uksort()用法及代码示例
- PHP ArrayIterator next()用法及代码示例
- PHP ArrayIterator key()用法及代码示例
- PHP ArrayIterator asort()用法及代码示例
- PHP ArrayIterator __construct()用法及代码示例
- PHP ArrayIterator append()用法及代码示例
- PHP ArrayIterator uasort()用法及代码示例
- PHP ArrayIterator serialize()用法及代码示例
- PHP ArrayIterator natsort()用法及代码示例
- PHP ArrayIterator ksort()用法及代码示例
- PHP ArrayIterator getFlags()用法及代码示例
- PHP ArrayIterator getArrayCopy()用法及代码示例
- PHP ArrayIterator current()用法及代码示例
- PHP ArrayIterator count()用法及代码示例
注:本文由纯净天空筛选整理自jit_t大神的英文原创作品 PHP | ArrayIterator uksort() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。