Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert Array to Params of Method?

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

}
like image 247
daGrevis Avatar asked Feb 23 '23 17:02

daGrevis


2 Answers

Use the function call_user_func_array.

For example:

call_user_func_array('bar', $params);
like image 66
ComFreek Avatar answered Mar 08 '23 01:03

ComFreek


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.

like image 30
Adrian World Avatar answered Mar 08 '23 01:03

Adrian World