我有以下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表示URL1,1表示description2,依此类推)。
如何让filter函数将整个对象传递给回调函数?因此,它将为项目中的每个对象/数组调用cleanProjects (在本例中为3次)。
发布于 2012-12-17 20:10:06
您现在使用的是只有PHP 5.4 above支持的Indirect method call by array variable
解决方案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);https://stackoverflow.com/questions/13913094
复制相似问题