AnnanFay
AnnanFay

Reputation: 9749

How can I call a PHP function with some of the values from an array?

Given the function bar takes a variable number of arguments. How can foo be implemented?

function foo($a, $b, $c) {
    return bar($a, $b, $c[0], $c[1], $c[2], ..., $c[n]);
}

Upvotes: 0

Views: 41

Answers (1)

hakre
hakre

Reputation: 197775

call_user_func_array allows you to call a function with an array of parameters. Create the parameter array and call the function:

$parameters = array_merge(array($a, $b), $c);
return call_user_func_array('bar', $parameters);

Upvotes: 1

Related Questions