如何调用PHP函数并将数组的每个项目作为不同的参数传递? [重复]
问题描述:
This question already has an answer here:
I'm trying to call a function and pass an infinite amount of parameters from an array. This is for use in an HMVC framework to call the controller and pass X amount of parameters so I also welcome any better approaches to this as well.
I'm calling my function like this where $params is an array of a potentially infinite amount of values:
controller::$action($params);
Then the params would be fully configurable by the programmer in the controller. This would be my example for the edit action:
public static function edit($id = false, $group = 'default', etc.){
// Something
}
</div>
答
function edit ($args) {
foreach ($args as $arg) {
echo "Arg = ".$arg;
}
}
$params = array($id, $group, etc...);
call_user_func_array("edit", array($params));