PHP usort / uasort没有正确排序多维数组

时间:2018-04-01 17:28:16

标签: php arrays multidimensional-array

我有这个数组:

Array
(
    [0] => Array
        (
            [pair] => BTCUSDT
            [profit] => 0.2313
        )

    [1] => Array
        (
            [pair] => ETHUSDT
            [profit] => 0.8372
        )

    [2] => Array
        (
            [pair] => NEOUSDT
            [profit] => 0.1832
        )

    [3] => Array
        (
            [pair] => BCCUSDT
            [profit] => 0.4372
        )

    [4] => Array
        (
            [pair] => XRPUSDT
            [profit] => 0.5372
        )

)

我希望使用以下代码根据利润数对该数组进行排序:

usort($trading_array, function($a, $b) {
    return $a['profit'] - $b['profit'];
});

但它不会按降序生成排序利润。事实上,似乎它随机化了我的数组。这是结果:

Array
(
    [4] => Array
        (
            [pair] => XRPUSDT
            [profit] => 0.5372
        )

    [3] => Array
        (
            [pair] => BCCUSDT
            [profit] => 0.4372
        )

    [2] => Array
        (
            [pair] => NEOUSDT
            [profit] => 0.1832
        )

    [1] => Array
        (
            [pair] => ETHUSDT
            [profit] => 0.8372
        )

    [0] => Array
        (
            [pair] => BTCUSDT
            [profit] => 0.2313
        )

)

0 个答案:

没有答案