排序数组 PHP usort

Sort Array PHP usort

我正在尝试按 $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';
    }
}

任何帮助都会很棒!

如果您的数组包含 key/index 那么,您可以使用 uasort() php 函数。

$sort = uasort($arrayForSort, function($a, $b)
        {
            return $b['weekrent'] - $a['weekrent'];
        }
);

如果你想对多个元素进行排序,那么你可以使用下面的代码:

$sort = uasort($arrayForSort, function($a, $b)
        {
            $c = $b['weekrent'] - $a['weekrent'];
            $c .= $b['id'] - $a['id'];
            return $c;
        }
);

然后你将不得不打印 arrayForSort 并得到结果。

printr($arrayForSort);