按键值对数组进行排序


Sort an Array by keys value

我有以下数组:

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
        )
)

我正在尝试按"十进制"键的值对其进行排序。

我创建了一个函数来按十进制值进行排序,但由于某种原因,正如您在上面的数组中看到的那样,它并没有完全排序,因为 0.86 比 0.85 更好的值。

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

谢谢。

你快到了。再次阅读usort()文档页面。它在关于参数value_compare_func描述的说明中说:

int callback ( mixed $a, mixed $b )

谨慎

从比较函数返回非整数值(如浮点数)将导致回调返回值的内部强制转换为整数。因此,诸如 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;
    }
}