I'm working with multi-dimensional arrays. Each array consists of properties along a street and one of the values is street address. I'd like to order the arrays such that all the odd addresses come before the even addresses. It's already ordered numerically (lowest to highest), so the only thing I'm trying to figure out is how to order the odds before the evens.
$array = [
[
'apn' => 345345345,
'sqft' => 1200,
'address' => '323 Pacific Ave.',
],
[
'apn' => 345345342,
'sqft' => 1421,
'address' => '324 Pacific Ave.',
],
[
'apn' => 345345346,
'sqft' => 1001,
'address' => '325 Pacific Ave.',
],
];
Use usort()
and define a custom sorting function:
usort($array, function($a, $b)
{
if ($a['apn'] % 2 == $b['apn'] % 2) {
if ($a['apn'] == $b['apn']) {
return 0;
}
return ($a['apn'] < $b['apn']) ? -1 : 1;
}
return ($a['apn'] % 2 == 0) ? 1 : -1;
});