将每个数组键作为参数传递给对象方法

时间:2016-01-22 01:29:25

标签: php arrays

我有一个关联数组,我希望将每个键作为参数传递给类方法,例如:

$arr = ['foo' => 'test', 'bar' => 'tested'];
$this->method($foo, $bar);
但是,根据每种情况,传递的变量数量可能会有所不同 我发现了一些类似的问题,但没有一个是我需要的。 那么,我该怎么做呢?

我想让数组的每个键成为变量并将其作为参数传递 $arr = ['foo' => 'bar']; >>> $foo = 'bar' and then $this->method($foo)

1 个答案:

答案 0 :(得分:1)

你可以使用Vitr更简单,更易读的答案,或者如果你必须明确,你可以使用call_user_func_array

call_user_func_array(array($this, 'method'), $arr);
相关问题