我正在使用多维数组。每个数组都包含街道上的属性,其中一个值是街道地址。
我想订购数组,使得每条街道的奇数地址都列在偶数地址之前。它已经按数字顺序排列(从最低到最高),所以我唯一想弄清楚的是如何在平均之前订购赔率。
$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.')
);
目前我有以下内容:
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;
});
问题在于它导致所有平均值之前列出的所有赔率,而不是每条街道在该街道前夕的所有赔率,如下所示:
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.
)
)
答案 0 :(得分:1)
那将是:
usort($array, function($x, $y)
{
$x = preg_split('/^(\d+)\s+(.*)/', $x['address'], -1 , 3);
$y = preg_split('/^(\d+)\s+(.*)/', $y['address'], -1 , 3);
$x[2] = $x[0] % 2;
$y[2] = $y[0] % 2;
if($x[1]==$y[1]) //first level: streets
{
if($x[2]==$y[2]) //second level: oddity
return $x[0]<$y[0]?-1:$x[0]!=$y[0]; //third level: numeric order
return $x[2]>$y[2]?-1:1;
}
return $x[1]<$y[1]?-1:1;
});
请注意,您的难度是因为您实际上有3个级别的排序:
小提琴可用here。此外,preg_split()
可能不是分隔数字和街道的最佳解决方案,但我不确定常见情况。对此有一点解释,-1
表示“对结果中的元素没有限制”,3
表示PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
(请查看我提供的链接以获取更多详细信息)。