(PHP 4, PHP 5)
krsort — 对数组按照键名逆向排序
&$array
[, int $sort_flags
= SORT_REGULAR
] )对数组按照键名逆向排序,保留键名到数据的关联。主要用于结合数组。
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Example #1 krsort() 例子
<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
krsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
以上例程会输出:
d = lemon c = apple b = banana a = orange
(2005-08-07 18:37:41)
To create a natural reverse sorting by keys, use the following function:
<?php
function natkrsort($array)
{
$keys = array_keys($array);
natsort($keys);
foreach ($keys as $k)
{
$new_array[$k] = $array[$k];
}
$new_array = array_reverse($new_array, true);
return $new_array;
}
?>
peter at pmkmedia dot com (2003-11-07 13:51:35)
lolo at phpheaven dot net (2000-11-28 12:33:54)
If you want to emulate the krsort function for an older version of php, you can use this piece of code:
function KeyComp($a, $b)
{
return -(strcmp($a,$b));
}
function krsort($MyArray)
{
uksort($MyArray, "KeyComp");
}
Maybe obvious and useless, but who knows...