【发布时间】:2016-03-08 09:54:57
【问题描述】:
我正在尝试按 $weekrent 对数组输出进行排序,到目前为止我已经得到了这个,但我得到的只是重复的相同数据而不是有序的数据。我还有其他方法可以做到这一点,以便我可以根据变量按顺序排列数据吗?
$values = $client->SearchProperties($parameters);
if(!is_array($values->SearchPropertiesResult->PropertyInfo))
{
$values->SearchPropertiesResult->PropertyInfo = array($values->SearchPropertiesResult->PropertyInfo);
}
if($values != '')
{
$arrayForSort = array();
foreach ($values->SearchPropertiesResult->PropertyInfo as $message)
{
$uglyid = $message->ID;
$id = $message->FriendlyID;
$mainphoto = $message->MainPhoto->PhotoUrl;
$furnished = $message->Furnished;
$addressline1 = $message->Address1;
$rooms = $message->MaxTenants;
$rent = $message->Rent;
$description = $message->Description;
$isletagreed = $message->IsLetAgreed;
$facilities = $message->Facilities->FacilityInfo;
$photos = $message->Photos->PhotoInfo;
$roomsinfo = $message->Rooms->RoomInfo;
$facilitiesstring = serialize($facilities);
$extractnumbers = ereg_replace("[^0-9]", "", $rent);
$monthrent = ($extractnumbers) / $rooms;
$monthrentrounded = number_format(($monthrent/100),2);
$weekrent = ($monthrentrounded) * 12 / 52;
$arrayForSort[] = array('weekrent' => $weekrent, 'message' => $message);
$weekrentrounded = floor($weekrent * 100) / 100;
$roomsinfojson = json_encode($roomsinfo);
$facilitiesjson = json_encode($facilities);
$roomsinfodouble = (substr_count(strip_tags($roomsinfojson),"Double"));
$roomsinfosingle = (substr_count(strip_tags($roomsinfojson),"Single"));
$roomsinfobathroom = (substr_count(strip_tags($roomsinfojson),"Bathroom"));
$roomsinfoshower = (substr_count(strip_tags($roomsinfojson),"Shower"));
$facilitiesparking = (substr_count(strip_tags($facilitiesjson),"Parking"));
$facilitiesgarden = (substr_count(strip_tags($facilitiesjson),"Garden"));
$totalbathrooms = $roomsinfobathroom + $roomsinfoshower;
$totalimages = count($photos);
}
foreach ($arrayForSort as $item)
{
usort($arrayForSort, function($a, $b) {
return $a['weekrent'] - $b['weekrent'];
});
$message = $item['message'];
echo '$addressline1';
}
}
任何帮助都会很棒!
【问题讨论】:
-
代码中的 $a 和 $b 是什么?
-
不需要对数组进行多次排序。删除
foreach ($arrayForSort as $item),只保留对它包含的usort()的调用。如果$arrayForSort没有正确排序,请确保您在'weekrent'中输入的值是正确的。我不会使用$monthrentrounded(它不是一个四舍五入的数字,而是一个字符串),而是只使用数字来计算$weekrent。如果您需要四舍五入,请使用相应的函数。它的名字是(还有什么?)round()。