开发者

Callback function for array_filter

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();

How to access the array being filtered within the callback function? And is it possible to pass one or two arguments to the callback function?

And I have PHP 5.3.1, just in case you'll need to know 开发者_StackOverflowthe version I am using.

EDIT: // separate formatting and then call array_unique


No need to reinvent the wheel here: http://php.net/array-unique


Try

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜