Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have really simple problem in my PHP script. There is a function defined which takes variable length argument list:

function foo() {
  // func_get_args() and similar stuff here
}

When I call it like this, it works just fine:

foo("hello", "world");

However, I have my variables in the array and I need to pass them "separately" as single arguments to the function. For example:

$my_args = array("hello", "world");
foo(do_some_stuff($my_args));

Is there any do_some_stuff function which splits the arguments for me so I can pass them to the function?

share|improve this question

7 Answers

up vote 9 down vote accepted

Use ReflectionFunction::invokeArgs(array $args) or call_user_func_array( callback $callback , array $param_arr).

share|improve this answer
 
+1 for Reflections. Although it's like an overkill here, but it's always cool to deal with them! –  dfsq Jan 28 '12 at 13:00
 
Thanks, both your solutions solve my problem perfectly. –  Pavel S. Jan 28 '12 at 13:08

Well you need call_user_func_array

call_user_func_array('foo', $my_args);

http://php.net/manual/en/function.call-user-func-array.php

share|improve this answer
 
Thanks for the answer, it's exactly what I need. However, I can accept only one answer. –  Pavel S. Jan 28 '12 at 13:03

Sounds to me like you are looking for call_user_func_array.

share|improve this answer
 
Thanks for the answer, it's exactly what I need. However, I can accept only one answer. –  Pavel S. Jan 28 '12 at 12:59
 
Just accept the one that either gives the most required detail, or the earliest one :) –  Cags Jan 28 '12 at 13:05

You are searching for call_user_func_array().

http://it2.php.net/manual/en/function.call-user-func-array.php

Usage:

$my_args = array("hello", "world");
call_user_func_array('foo', $my_args);

// Equivalent to:
foo("hello", "world");
share|improve this answer
 
Thank you for the comparison –  jocken Oct 10 '12 at 13:41

http://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list

Isn't this what you want?

edit ah... ok... how about this: Passing an Array as Arguments, not an Array, in PHP

share|improve this answer
 
Nope. I have already read this. All the functions are supposed to work inside my "foo" function. I need to split the arguments before they are passed to the function. –  Pavel S. Jan 28 '12 at 12:56

If you can change the code of foo() it should be easy to solve this in just one place.

function foo()
{
    $args = func_get_args();
    if(count($args) == 1 && is_array($args[0]))
    {
        $args = $args[0]
    }
    // use $args as normal
}
share|improve this answer
 
This not works if he wants to pass an array (for real) as first argument. –  lorenzo-s Jan 28 '12 at 13:00
 
No, it does not work if there's only one argument and that argument is supposed to be an array. If there's more than one argument then they're all left alone. –  Arjan Jan 28 '12 at 13:04
 
Yup, I saw, I saw... –  lorenzo-s Jan 28 '12 at 13:05

This solution is not recommended at all, but just showing a possibility :

Using eval

eval ( "foo('" . implode("', '", $args_array) . "' )" );

share|improve this answer
1  
I know it's only "showing a possibility", but still... it's eval. ;) –  Crozin Jan 28 '12 at 13:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.