1 回答

TA贡献1998条经验 获得超6个赞
您可以使用call_user_func_array()通过数组传递参数:
$f = ['foo', 'param1', 'param2'];
$func = array_shift($f); // remove function name.
call_user_func_array($func, $f);
(将上面的示例代码与下面的函数一起使用...)
要获取函数参数的任意列表,请使用func_get_args()
function foo() {
$args = func_get_args();
print_r($args);
}
您甚至不必指定参数,只需传递任意数量的参数即可:
foo(1, 2, 4, 'hello', 1.234, ['foo', 'bar']);
产生输出
Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => hello
[4] => 1.234
[5] => Array
(
[0] => foo
[1] => bar
)
)
https://www.php.net/manual/en/function.func-get-args
https://www.php.net/manual/en/function.call-user-func-array
希望有帮助
- 1 回答
- 0 关注
- 93 浏览
添加回答
举报