0

I've been looking but unfortunately not found anything useful.

I have a function

function myFunc ($param1, $param2){ ...

I need to call that function but I have the parameters in an array

$params = [$param1, $param2]

Is there any way of doing something like ...[] (like in Js) ?

Note: I can not do

myFunc($params[$param1], $params[$param2]);

2 Answers2

5

Since php5.6 there's a ... syntax:

myFunc(...$params);

And of course, call_user_func_array still exists and works both in php5 and php7:

call_user_func_array('myFunc', $params);
u_mulder
  • 54,101
  • 5
  • 48
  • 64
1

You can use the function call_user_func_array documentation: http://php.net/manual/en/function.call-user-func-array.php

$params = [$param1, $param2]

function myFunc ($param1, $param2){
    echo $param1;
    echo $param2;
}

call_user_func_array('myFunc', $params);
MrChrissss
  • 271
  • 1
  • 2
  • 11