I have an array that looks similar to this:
Array
(
[0] => stdClass Object
(
[Leasing] => 12939.74
[Name] => Jeremy
[Rental] => 0
[Sales] => 56603.13
[Total] => 69542.87
)
[1] => stdClass Object
(
[Leasing] => 0
[Name] => Shaun
[Rental] => 0
[Sales] => 58590
[Total] => 58590
)
[2] => stdClass Object
(
[Leasing] => 0
[Name] => Lindsay
[Rental] => 0
[Sales] => 22951.97
[Total] => 22951.97
)
[3] => stdClass Object
(
[Leasing] => 0
[Name] => Sally
[Rental] => 1200
[Sales] => 21624.9
[Total] => 22824.9
)
[4] => stdClass Object
(
[Leasing] => 0
[Name] => House
[Rental] => 0
[Sales] => 16235.81
[Total] => 16235.81
)
[5] => stdClass Object
(
[Leasing] => 5298.85
[Name] => Bill
[Rental] => 1200
[Sales] => 0
[Total] => 6498.85
)
)
Currently, the array is sorted by total using this:
usort($data, function ($a, $b) {
return $b->Total - $a->Total;
});
Now, I need to be able to ALWAYS have the person with [Name] => House to the top of the array. My thoughts are that I can leave it sorted by Total (because I still need it that way) and then take the element with the House value and put it at the start of the array. I can take a particular KEY and put it at the top, but the KEY’s may change depending on who has the highest total. How can I always put the person named House at the top of the array?
This should work:
From PHP: usort – Manual:
In this case, return
1tells the sorting function thatHouseis greater than any other value, and-1thatHouseis lesser than any other value.