Link to home
Start Free TrialLog in
Avatar of Paul Konstanski
Paul KonstanskiFlag for United States of America

asked on

Convert elements of array into arguments to be passed into a PHP function.

I have a situation where I need to pass a variable number of arguments into a function that I don't have control over.

The function has this format:  setFunction ($type, $var1, [$var2], [$var3]...)  where $type is a setting ('left", "right", "center"). There must be at least 1 variable, but you can have as many as you want.  

This normally is set at the time of calling the function by setting the variables like this.
$type = "left";
$var1 = "10";
$var2 = "abcdef";
$var3 = "255";

$display = setFunction($type, $var1, $var2, $var3);

Open in new window

What I'm wanting to do is be able to create the additional arguments on the fly from an array.  So in the above example it would be like this:
$type = "left";
$myvars = array("10", "abcdef", "255");

// here is where I want some magic to happen that would create the variables that would then be appended to the function call.
// so you simply build the array and it creates the $var1, $var2, $var3 or how ever many variables you have based on the number of elements in the array.

$display = setFunction($type, magic variable variables here);

Open in new window

I'm trying to create a shortcut so I can call a function to build my setFunction call from an array rather than have to always declare and build it each time.

Thanks.
SOLUTION
Avatar of themrrobert
themrrobert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Paul Konstanski

ASKER

All gave some good insight into my situation.