12

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?

Jigar
  • 129
  • 5
Pavel S.
  • 11,892
  • 18
  • 75
  • 113
  • possible duplicate of [Passing an Array as Arguments, not an Array, in PHP](http://stackoverflow.com/questions/744145/passing-an-array-as-arguments-not-an-array-in-php) – DanMan Feb 18 '14 at 14:31
  • Use this answer. This is much more accurate. https://stackoverflow.com/a/34049110/2147023 – theBuzzyCoder Jun 23 '18 at 09:14
  • Does this answer your question? [unpacking an array of arguments in php](https://stackoverflow.com/questions/294313/unpacking-an-array-of-arguments-in-php) – Anderson Green Aug 16 '21 at 14:08

8 Answers8

17

Use

or

Aubin
  • 14,617
  • 9
  • 61
  • 84
Crozin
  • 43,890
  • 13
  • 88
  • 135
  • +1 for `Reflection`s. Although it's like an overkill here, but it's always cool to deal with them! – dfsq Jan 28 '12 at 13:00
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

dfsq
  • 191,768
  • 25
  • 236
  • 258
3

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");
lorenzo-s
  • 16,603
  • 15
  • 54
  • 86
2

Sounds to me like you are looking for call_user_func_array.

Peter O'Callaghan
  • 6,181
  • 3
  • 26
  • 27
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

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

Community
  • 1
  • 1
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • 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
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
}
Arjan
  • 9,784
  • 1
  • 31
  • 41
  • 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
0

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

Using eval

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

Pheonix
  • 6,049
  • 6
  • 30
  • 48