21

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/...

2 Answers 2

25

You need to use call_user_func_array

call_user_func_array( array($method, $function), $params);
Sign up to request clarification or add additional context in comments.

Comments

20

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);

Comments

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.