问题
I have a parameters array:
$params[1] = 'param1';
$params[2] = 'param2';
$params[3] = 'param3';
...
$params[N] = 'paramN';
I have a caller to various functions:
$method->$function( $params );
How can I parse the $params array, so multiple (and unlimited) parameters can be passed to any function:
$method->$function( $params[1], $params[2], ..., $params[N] );
The idea is to utilize the url rewrite like this:
http://domain.com/class/method/parameter/parameter/parameter/...
回答1:
You need to use call_user_func_array
call_user_func_array( array($method, $function), $params);
回答2:
As of PHP 5.6 you can use argument unpacking:
function add($a, $b, $c) {
return $a + $b + $c;
}
$numbers = [1, 2, 3];
echo add(...$numbers);
来源:https://stackoverflow.com/questions/2720318/how-to-pass-array-as-multiple-parameters-to-function