2
長話短說,我需要使用usort對一個對象數組進行排序,並且我需要告訴usort要排序的對象中的哪些字段。使用帶條件結果的PHP usort
顯而易見的解決方案是創建數十個獨立的usort排序函數,但這似乎是多餘和醜陋的。大多數情況下,對象將通過$ _GET的輸入進行排序,但並非總是如此,所以我不想直接按$ _GET變量進行排序。
usort函數可以使用當前類的排序函數嗎?這樣
<?php
class myClass
{
public $myArray;
private $by;
public function filter($by)
{
$this->by = $by;
usort($this->myArray, /* Somehow point to the following function: */);
}
private function srt($a, $b)
{
$c = $this->by; // <- reaching a third variable
// ...
}
}
?>
+1。非常好的主意。 – pltvs 2010-09-24 09:10:27