Question
I am trying to sort array with usort, but it doesn't work, What am i doing wrong?
$tmp = array();
$tmp[] = array(
'x' => 0.000123
);
$tmp[] = array(
'x' => 0.000120
);
$tmp[] = array(
'x' => 0.000333
);
usort($tmp, function ($a, $b) {
return $b['x'] - $a['x'];
});
print_r($tmp);
Solution
$tmp = array();
$tmp[] = array(
'x' => 0.000123
);
$tmp[] = array(
'x' => 0.000120
);
$tmp[] = array(
'x' => 0.000333
);
usort($tmp, function ($a, $b) {
$a = $a['x'];
$b = $b['x'];
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
});
Try this one
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow