-1
我想排序我的数组输出$ weekrent,我有这个到目前为止,但我得到输出是重复相同的数据,而不是它的顺序。有没有其他方法可以做到这一点,以便我可以根据变量排列数据?排序数组PHP PHP usort
$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()'](http://php.net/manual/en/function.round.php)。 – axiac