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 for each street, the odd addresses are listed 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 = array(
array('apn' => 345345345, 'sqft' => 1200, 'address' => '323 Pacific Ave.'),
array('apn' => 345345342, 'sqft' => 1421, 'address' => '324 Pacific Ave.'),
array('apn' => 345345346, 'sqft' => 1012, 'address' => '325 Pacific Ave.'),
array('apn' => 345345347, 'sqft' => 1093, 'address' => '328 Pacific Ave.'),
array('apn' => 345345353, 'sqft' => 1121, 'address' => '12 Lincoln Ave.'),
array('apn' => 345345351, 'sqft' => 1643, 'address' => '13 Lincoln Ave.'),
array('apn' => 345345352, 'sqft' => 1222, 'address' => '14 Lincoln Ave.')
);
Currently I have the following:
usort($array, function($a, $b)
{
if ($a['address'] % 2 == $b['address'] % 2) {
if ($a['address'] == $b['address']) {
return 0;
}
return ($a['address'] < $b['address']) ? -1 : 1;
}
return ($a['address'] % 2 == 0) ? 1 : -1;
});
The problem is that it results in all the odds listed before all the evens, rather than all the odds of each street before the eves for that street, as follows:
Array
(
[0] => Array
(
[apn] => 345345341
[sqft] => 1001
[address] => 13 Lincoln Ave.
)
[1] => Array
(
[apn] => 345345341
[sqft] => 1001
[address] => 12 Lincoln Ave.
)
[2] => Array
(
[apn] => 345345341
[sqft] => 1001
[address] => 14 Lincoln Ave.
)
[3] => Array
(
[apn] => 345345345
[sqft] => 1200
[address] => 323 Pacific Ave.
)
[4] => Array
(
[apn] => 345345341
[sqft] => 1001
[address] => 325 Pacific Ave.
)
[5] => Array
(
[apn] => 345345342
[sqft] => 1421
[address] => 324 Pacific Ave.
)
[6] => Array
(
[apn] => 345345341
[sqft] => 1001
[address] => 328 Pacific Ave.
)
)