我有以下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);
过滤其中的字段= array('projects' => array('filter' => FILTER_CALLBACK,'flags' => FILTER_FORCE_ARRAY, 'options' => array($this, 'cleanProjects'));
但是,传递给cleanProjects函数的值不是包含描述和路径的数组,但是它会逐个传递所有值(因此该方法被调用六次,1用于描述1,1用于url 1) ,1为描述2等。)
如何让过滤函数将整个对象传递给回调函数? 因此,它会为项目中的每个对象/数组调用cleanProjects(在本例中为3次)。
答案 0 :(得分:0)
您现在使用的名称为Indirect method call by array variable,仅受PHP 5.4 above
解决方案1:升级您的PHP版本,您的代码无需修改即可使用
'options' => array($this, 'cleanProjects')));
|_______________________|
+------------ Indirect Method call by array
解决方案2:只需使用Closures
$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);