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.

Paul KonstanskiProject SpecialistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This is easy, all you need to do is make sure that type is the first argument of the array. This can be done with array_unshift, and the 'magic' is done with call_user_func_array

Like this:
$type = "left";
$args = array("arg1","arg2","10"); //build array
array_unshift($args,$type); //add $type to the front of the array

$display = call_user_func_array('setFunction',$args); //calls setFunction() with each $args item as it's own variable

Open in new window

Inside your function, you can call func_get_args() to get an array of all parameters passed in:

echo MyDynamicFunction("foo"); // Will echo 1
echo MyDynamicFunction("foo","bar","hello world"); // Will echo 3

function MyDynamicFunction()
  $args = func_get_args();
  return count($args);

Open in new window

You can use array_shift() or array_pop() or other normal array functions from that point on. Another example:

echo DoMath("sum",20,15); // 35
echo DoMath("sum",1,2,3,4); // 10
echo DoMath("divide",10,2); // 5

function DoMath()
  $args = func_get_args();
  $math_operation = array_shift($args);
  if(!in_array($math_operation, array("sum","divide")))
    return "First parameter must be sum or divide!";
  if($math_operation == "sum")
    $sum = 0;
    foreach($args as $number)
      $sum += $number;
    $result = $sum;
  elseif($math_operation == "divide")
    $result = $args[0] / $args[1];

  return $result;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
You might want to consider this kind of design pattern.  It lets you give names and default values to the function arguments.  It uses a RequestArguments class to achieve something that is not available in the native PHP code.
Paul KonstanskiProject SpecialistAuthor Commented:
All gave some good insight into my situation.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.