1

我有以下 POST 数组:

[projects] => Array (
        [0] => Array
            (
                [description] => description 1
                [path] => url 1
            )

        [1] => Array
            (
                [description] => description2
                [path] => url 2
            )

        [2] => Array
            (
                [description] => description 3
                [path] => url 3
            )

    )

我希望用filter_var_array($_POST, $this -> fields);where fields =过滤它array('projects' => array('filter' => FILTER_CALLBACK,'flags' => FILTER_FORCE_ARRAY, 'options' => array($this, 'cleanProjects'));

但是,传递给 cleanProjects 函数的值并不是包含描述和路径的数组,而是将所有的值 1 一个一个地传递(所以该方法被调用了六次,1 代表描述 1,1 代表 url 1,1 代表描述2等)

如何让过滤函数将整个对象传递给回调函数?所以它会为项目中的每个对象/数组调用 cleanProjects(在这个例子中是 3 次)。

4

1 回答 1

0

您现在使用的称为数组变量的间接方法调用,它仅支持PHP 5.4 above

解决方案 1:升级您的 PHP 版本,您的代码无需修改即可工作

        'options' => array($this, 'cleanProjects')));  
                         |_______________________|
                                      +------------ Indirect Method call by array

解决方案2:只需使用闭包

    $self = $this ;
    $options = function($args) use ($self)
    {
        $self->cleanProjects($args);
    };

    $this->fields = array('projects' => array(
            'filter' => FILTER_CALLBACK,
            'flags' => FILTER_FORCE_ARRAY, 
            'options' => $options));   // add the closure 
    $var = filter_var_array($_POST, $this -> fields);
于 2012-12-17T12:10:05.640 回答