我有一個數組,看起來類似於:移動數組元素頂部陣列的
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
)
)
目前,該陣列是由總使用該排序:
usort($data, function ($a, $b) {
return $b->Total - $a->Total;
});
現在,我需要能夠始終將具有[Name] => House
的人員置於陣列的頂部。我的想法是,我可以讓它按Total
排序(因爲我仍然需要它),然後將這個元素與House值一起放到數組的開頭。我可以把一個特定的KEY放在最上面,但是KEY可能會根據誰的總數最高而改變。我怎樣才能始終將名爲House
的人放在數組的頂部?
一種方法:將所需的第一個元素放置在第一個位置,將數組拆分成一個元素和其他所有元素,然後對所有其他部分進行排序。對不起,我不記得PHP的語法足以提供正確的答案。 – GreenMatt
您的排序函數的返回值將通過usort轉換爲整數,所以它會看到0.05和0.95相等。 – Sammitch