使用usort函数并不复杂。
以下是我之前的代码:
function merchantSort($a,$b){
return ....// stuff;
}
$array = array('..','..','..');
为了排序,我只需要执行以下操作:
usort($array,"merchantSort");
现在我们正在升级代码,并将所有全局函数放在它们适当的位置。现在所有的代码都在一个类中了,我无法弄清如何使用usort函数根据对象方法而不是简单函数对数组进行排序。
class ClassName {
...
private function merchantSort($a,$b) {
return ...// the sort
}
public function doSomeWork() {
...
$array = $this->someThingThatReturnAnArray();
usort($array,'$this->merchantSort'); // ??? this is the part i can't figure out
...
}
}
问题是如何在usort()函数内调用对象方法
$this
(很酷)。 - Benusort($array, 'ClassName:merchantSort')
即可,对吗? - caw'self::merchantSort'
并且能够正常工作。 - Pere