我有一个像这样的数组
array(
[0] => Array (
[fullAddress] => 2482 Midvale St,Kettering, OH, 45420
[bedrooms] => 3
[bathrooms] => 1.0
[sqft] => 1548
[yearBuilt] => 1955
[lastSoldDate] => 12/09/2015
[lastSoldPrice] => 87,000
[distance] => 0.59
)
[1] => Array (
[fullAddress] => 2828 Vale Dr, Dayton, OH, 45420
[bedrooms] => 3
[bathrooms] => 1.5
[sqft] => 1356
[yearBuilt] => 1956
[lastSoldDate] => 10/09/2015
[lastSoldPrice] => 117,500
[distance] => 0.38
)
[2] => Array (
[fullAddress] => 2812 Vale Dr, Dayton, OH, 45420
[bedrooms] => 4
[bathrooms] => 2.0
[sqft] => 1248
[yearBuilt] => 1955
[lastSoldDate] => 05/13/2015
[lastSoldPrice] => 114,900
[distance] => 0.33
)
)
我需要订购:距离asc,lastSoldPrice desc(最近的日期到当前日期)和最后的订单,相同的卧室,浴室和平方英尺在20%+ - 。与这样的主要财产相比:
Array (
[fullAddress] => 2400 Ghent Ave, Dayton, OH 45420
[bedrooms] => 3
[bathrooms] => 1.0
[sqft] => 984
[yearBuilt] => 1956
[lastSoldDate] => 05/06/2014
[lastSoldPrice] => 74,763
)
所以前两个订单已经准备就绪,我可以通过距离和lastsoldprice与usort()订购但是我还没有得到最后一个订单因为我不知道如何与另一个数组进行比较(主要财产)。
这是我与usort
一起使用的代码function sort($a, $b) {
if ($a['distance']==$b['distance']) {
if ($a['lastSoldDate']==$b['lastSoldDate']) {
return 0;
} else {
return ($a['lastSoldDate']<$b['lastSoldDate']) ? 1 : -1;
}
} else {
return ($a['distance']>$b['distance']) ? 1 : -1;
}
}
usort($propertydata, 'sort');
我希望有人可以帮助我。感谢
答案 0 :(得分:0)
为此,您需要先按平方英尺对数组进行排序,然后按距离和最后售价进行排序。
喜欢这样
function sort1($a, $b) {
if ($a['bedrooms']!=$b['bedrooms'] || $a['bathrooms']!=$b['bathrooms'] || $a['sqft']==$b['sqft']) {
return 0;
} else {
return (abs($a['sqft']-$b['sqft'])<$a['sqft']*.2) ? -1 : 1;
}
}
function sort2($a, $b) {
if ($a['distance']==$b['distance']) {
if ($a['lastSoldDate']==$b['lastSoldDate']) {
return 0;
} else {
return ($a['lastSoldDate']<$b['lastSoldDate']) ? 1 : -1;
}
} else {
return ($a['distance']>$b['distance']) ? 1 : -1;
}
}
usort($propertydata, 'sort1');
usort($propertydata, 'sort2');