class MyClass {
private function isExist($arr) {
// need to do some formatting here first
// need to call in_array here to check whether to filter out duplicates
return false; // temporary return value
}
public function test() {
$data = array(
array('foo' => 'alpha', 'bar' => 'bravo'),
array('foo' => 'charlie', 'bar' => 'delta'),
array('foo' => 'alpha', 'bar' => 'bravo'),
);
$result = array_filter($data, array('MyClass', 'isExist'));
print_r($result);
}
}
$obj = new MyClass();
$obj->test();
如何在回调函数中访问被过滤的数组?是否可以将一个或两个参数传递给回调函数?
我有PHP 5.3.1,以防万一你需要知道我正在使用的版本。
编辑: //单独格式化,然后调用array_unique
答案 0 :(得分:1)
无需重新发明轮子:http://php.net/array-unique
答案 1 :(得分:0)
尝试
class MyClass {
...
public function test() {
$data = array(
array('foo' => 'alpha', 'bar' => 'bravo'),
array('foo' => 'charlie', 'bar' => 'delta'),
array('foo' => 'alpha', 'bar' => 'bravo'),
);
$result = array_filter($data, array('MyClass', 'isExist'));
return $result;
}
}
$obj = new MyClass();
$array_filterd = $obj->test();