8

Is it possible to have an array and pass it into a function as separate arguments?

$name = array('test', 'dog', 'cat');
$name = implode(',' $name);
randomThing($name);

function randomThing($args) {
    $args = func_get_args();
    // Would be 'test', 'dog', 'cat'

    print_r($args);
}
0

2 Answers 2

12

No. That's what call_user_func_array() is for.

Sign up to request clarification or add additional context in comments.

Comments

9

As of PHP 5.6 you can use ... to pass an array as function arguments. See this example from the PHP documentation:

function add($a, $b) {
    return $a + $b;
}

echo add(...[1, 2])."\n";

$a = [1, 2];
echo add(...$a);

1 Comment

"... can also be used when calling functions to unpack an array or Traversable variable or literal into the argument list"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.