13

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?

3

8 Answers 8

17

Use

or

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for Reflections. Although it's like an overkill here, but it's always cool to deal with them!
6

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

1 Comment

Thanks for the answer, it's exactly what I need. However, I can accept only one answer.
3

Sounds to me like you are looking for call_user_func_array.

2 Comments

Thanks for the answer, it's exactly what I need. However, I can accept only one answer.
Just accept the one that either gives the most required detail, or the earliest one :)
3

You are searching for call_user_func_array().

https://www.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");

1 Comment

This is far better than to use Reflection in my opinion
0

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

1 Comment

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.
0

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
}

2 Comments

This not works if he wants to pass an array (for real) as first argument.
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.
0

I know it's an old question but it still comes up as the first search result - so here is an easier way;

<?php
function add(... $numbers) {
    $result=0;
    foreach($numbers as $number){
      $result+=intval($number);
    }
    return $result;
}

echo add(...[1, 2])."\n";

$a = [1, 2];
echo add(...$a);
?>

Source: https://www.php.net/manual/en/functions.arguments.php#example-142

Comments

-1

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

Using eval

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

1 Comment

I know it's only "showing a possibility", but still... it's eval. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.