2
PHP에서 usort()
함수를 사용하려고합니다. 비교 함수를 호출하는 방법을 잘 모르겠습니다. 여기에 내 코드가있다. 나는 $this->comparator
을 시도하고 그 didnt 도움. comparator
이 클래스의 멤버 변수에 액세스 할 필요가없는 함수 인 경우 간단합니다.PHP의 소트 비교 함수가 클래스 멤버 변수에 대한 액세스 필요 -
class A {
$p1 // non-associative array
$p2 // non-associative array
public function comparator($a, $b)
{
// the usual comparison stuff
if ($this->p1[$a] == $this->p2[$b])
return 0;
else ($this->p1[$a] < $this->p2[$b])
return 1;
else
return -1;
}
public function sorting()
{
// after some code
$some_array = array(..);
usort($some_array, "comparator") // <--- ERROR here: does not recognize comparator
}
}