Reputation: 3857
I have the following array :
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
)
)
and I am attempting to sort it by the "Decimal" key's value.
I have created a function to do a sort by the Decimal value, But for some reason as you can see by my above array, It isn't quite sorting it, In that 0.86 is a better value than 0.85.
usort($outcomes, 'sort_by_decimal');
function sort_by_decimal ($a, $b)
{
return $a['Decimal'] - $b['Decimal'];
}
Thank You.
Upvotes: 0
Views: 60
Reputation: 72226
You are almost there. Read the usort()
documentation page again. It says in a Note on the description of parameter value_compare_func
:
int callback ( mixed $a, mixed $b )
Caution
Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.
Now I thinks it's clear than your function should read something like:
function sort_by_decimal ($a, $b)
{
if ($a['Decimal'] < $b['Decimal']) {
return -1;
} elseif ($a['Decimal'] > $b['Decimal']) {
return +1;
} else {
return 0;
}
}
Upvotes: 3