关联数组排序数字

时间:2013-02-22 15:21:57

标签: arrays sorting associative

我有一个以下格式的关联数组。我需要按降序对数组进行排序,并按照升序对数组进行排序。

$numArray = array();
$numArray[0]['Word'] = 'One';
$numArray[0]['Number'] = 10.00;
$numArray[1]['Word'] = 'Two';
$numArray[1]['Number'] = 10.00;
$numArray[2]['Word'] = 'Three';
$numArray[2]['Number'] = 10.00; 
$numArray[3]['Word'] = 'Four';
$numArray[3]['Number'] = 30.00;
$numArray[4]['Word'] = 'Five';
$numArray[4]['Number'] = 20.00; 
$numArray[5]['Word'] = 'Six';
$numArray[5]['Number'] = 10.00; 
$numArray[6]['Word'] = 'Seven';
$numArray[6]['Number'] = 20.00; 
$numArray[7]['Word'] = 'Eight';
$numArray[7]['Number'] = 20.00;

请帮我按以下顺序对数组进行排序。

阵         (             [0] =>排列                 (                     [Word] =>四                     [编号] =>三十                 )

        [1] => Array
            (
                [Word] => Five
                [Number] => 20
            )

        [2] => Array
            (
                [Word] => Seven
                [Number] => 20
            )

        [3] => Array
            (
                [Word] => Eight
                [Number] => 20
            )

        [4] => Array
            (
                [Word] => One
                [Number] => 10
            )

        [5] => Array
            (
                [Word] => Two
                [Number] => 10
            )

        [6] => Array
            (
                [Word] => Three
                [Number] => 10
            )

        [7] => Array
            (
                [Word] => Six
                [Number] => 10
            )

    )

1 个答案:

答案 0 :(得分:0)

foreach ($numArray as $key => $row) {
    $array[$key]  = $row['Number']; 
}


array_multisort($array, SORT_DESC, $numArray);

这个小的PHP代码至少会根据Number对你的数组desc进行排序。 猜测那是你想要的一半。