-1

如何对下面的数组进行排序,以便顶部的较大值和键不会更改。

Array
(
    [8] => 2
    [9] => 2
    [10] => 1
    [12] => 1
    [16] => 1
    [17] => 1
    [18] => 1
    [19] => 1
    [20] => 2
    [23] => 1
    [24] => 2
    [25] => 2
    [27] => 1
    [50] => 2
    [4] => 1
    [14] => 1
)

谢谢

4

2 回答 2

0

您应该可以使用asort/ arsortarsort来自 PHP.net ( http://www.php.net/manual/en/function.arsort.php )的示例用法:

<?php
    $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
    arsort($fruits);
    foreach ($fruits as $key => $val) {
        echo "$key = $val\n";
    }
?>
于 2013-03-27T16:51:48.197 回答
0

直接来自PHP 手册

This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant.

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?> 
于 2013-03-27T16:52:06.317 回答