-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