按键值排序数组

时间:2015-01-19 10:56:43

标签: php arrays sorting

我有以下数组:

Array
(
    [0] => Array
        (
            [Outcome] => 1
            [Bookmaker] => PaddyPower
            [Updated] => 2015-01-19T09:28:36
            [Bet] => 1
            [SP] => 4/5
            [CP] => 17/20
            [line] => 
            [Decimal] => 0.85
        )

    [1] => Array
        (
            [Outcome] => 1
            [Bookmaker] => BetFair
            [Updated] => 2015-01-19T09:59:12
            [Bet] => 1
            [SP] => 1/25
            [CP] => 43/50
            [line] => 
            [Decimal] => 0.86
        )

    [2] => Array
        (
            [Outcome] => 1
            [Bookmaker] => BetVictor
            [Updated] => 2015-01-19T10:29:38
            [Bet] => 1
            [SP] => 869/1000
            [CP] => 869/1000
            [line] => 
            [Decimal] => 0.87
        )

    [3] => Array
        (
            [Outcome] => 1
            [Bookmaker] => BetFred
            [Updated] => 2015-01-19T09:28:30
            [Bet] => 1
            [SP] => 4/5
            [CP] => 17/20
            [line] => 
            [Decimal] => 0.85
        )

    [4] => Array
        (
            [Outcome] => 1
            [Bookmaker] => CoralBet
            [Updated] => 2015-01-19T09:28:36
            [Bet] => 1
            [SP] => 73/100
            [CP] => 83/100
            [line] => 
            [Decimal] => 0.83
        )

    [5] => Array
        (
            [Outcome] => 1
            [Bookmaker] => Bet365
            [Updated] => 2015-01-19T09:35:59
            [Bet] => 1
            [SP] => 73/100
            [CP] => 83/100
            [line] => 
            [Decimal] => 0.83
        )

    [6] => Array
        (
            [Outcome] => 1
            [Bookmaker] => WilliamHill
            [Updated] => 2015-01-19T09:48:17
            [Bet] => 1
            [SP] => 4/5
            [CP] => 83/100
            [line] => 
            [Decimal] => 0.83
        )

    [7] => Array
        (
            [Outcome] => 1
            [Bookmaker] => SkyBet
            [Updated] => 2015-01-19T09:29:48
            [Bet] => 1
            [SP] => 67/100
            [CP] => 83/100
            [line] => 
            [Decimal] => 0.83
        )

    [8] => Array
        (
            [Outcome] => 1
            [Bookmaker] => Ladbrokes
            [Updated] => 2015-01-19T09:29:48
            [Bet] => 1
            [SP] => 3/4
            [CP] => 4/5
            [line] => 
            [Decimal] => 0.8
        )

)

我试图通过" Decimal"对它进行排序。关键的价值。

我已经创建了一个按Decimal值排序的函数,但由于某种原因你可以通过上面的数组看到,它并没有对它进行排序,因为0.86是一个比0.85更好的值。

usort($outcomes, 'sort_by_decimal');
function sort_by_decimal ($a, $b)
{
    return $a['Decimal'] - $b['Decimal'];
}

谢谢。

1 个答案:

答案 0 :(得分:3)

你快到了。再次阅读usort() documentation page。它在 Note 中对参数value_compare_func

的说明进行了说明
  

int callback ( mixed $a, mixed $b )

     

<强>注意

     

从比较函数返回非整数值(例如float)将导致内部强制转换为回调值的整数。因此,诸如0.99和0.1之类的值都将被转换为整数值0,这将将这些值比较为相等。

现在我认为它比你的功能更清楚:

function sort_by_decimal ($a, $b)
{
    if ($a['Decimal'] < $b['Decimal']) {
        return -1;
    } elseif ($a['Decimal'] > $b['Decimal']) {
        return +1;
    } else {
        return 0;
    }
}