如何按降序对关联数组排序


How to sort associative arrays in descending order?

我知道rsort和array_reverse,但我想知道如何反转关联数组的所有元素以及它们的索引号。例如:

$age = array("x"=>"35", "y"=>"45", "z"=>"55");

并显示为

z 55

y 45

x 35

我试过了,但它没有显示我想要的:

$age = array("x"=>"35", "y"=>"45", "z"=>"55");
array_reverse($age);
foreach($age as $x => $x_value){
    echo $x . " " . $x_value;
    echo "<br>";
}

我认为你想按键按降序排序,所以你需要做流动。因为array_reverse()函数交换键与数组中的值。

$age = array("x"=>"35", "y"=>"45", "z"=>"55");
krsort($age);
foreach($age as $x => $x_value){
 echo $x . " " . $x_value;
echo "<br>";
}