Reputation: 21333
I need to pass params (like: 'param1', 'param2', 'param3'
) to the method... but I have array of params (like: array('param1', 'param2', 'param3')
). How to convert array to params?
function foo(array $params) {
bar(
// Here should be inserted params not array.
);
}
Upvotes: 3
Views: 5264
Reputation: 329
You can convert one to the other like this:
function foo(array $params)
{
bar(...$params);
}
or go straight to bar:
bar(...$array);
For the "bar" function to receive several parameters you can use:
function bar(...$params)
{
//$params is a array of parameters
}
or
function bar($p1, $p2, $p3)
{
//$pn is a variables
}
or
function bar($p1, ...$pn)
{
//$p1 is a variable
//$pn is a array of parameters
}
but a lot of attention with the types and quantity of parameters to not give you problems.
visite: Function Arguments
Upvotes: 1
Reputation: 3128
If you know the param names you could to the following
$params = array(
'param1' => 'value 1',
'param2' => 'value 2',
'param3' => 'value 3',
);
function foo(array $someParams) {
extract($someParams); // this will create variables based on the keys
bar($param1,$param2,$param3);
}
Not sure if this is what you had in mind.
Upvotes: 1
Reputation: 29424
Use the function call_user_func_array.
For example:
call_user_func_array('bar', $params);
Upvotes: 5