My array:
$data = [
['id' => 1, 'value' => 1.2],
['id' => 3, 'value' => 'N/A'],
['id' => 2, 'value' => 2.5],
];
I want sort the rows with numeric value
values precede rows with non-numeric value
values. (2.5 -> 1.2 -> 'N/A'):
My code:
usort($data, function($a, $b) {
if ($a['value'] == 'N/A') {
return $b['value'];
} elseif ($b['value'] == 'N/A') {
return $a['value'];
}
return strcmp($b['value'], $a['value']);
});
Desired result:
[
['id' => 2, 'value' => 2.5],
['id' => 1, 'value' => 1.2],
['id' => 3, 'value' => 'N/A'],
]
Try this:
Output:
Here we use array_multisort function.