PHP - 将数组作为可变长度参数列表传递

时间:2022-10-16 21:25:22

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

我的PHP脚本中有一个非常简单的问题。定义了一个带有可变长度参数列表的函数:

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?

是否有任何do_some_stuff函数为我分割参数,所以我可以将它们传递给函数?

7 个解决方案

#1


17  

Use

使用

or

要么

#2


6  

Well you need call_user_func_array

那你需要call_user_func_array

call_user_func_array('foo', $my_args);

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

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

#3


3  

You are searching for call_user_func_array().

您正在搜索call_user_func_array()。

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

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");

#4


2  

Sounds to me like you are looking for call_user_func_array.

听起来像你正在寻找call_user_func_array。

#5


0  

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

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

编辑啊......好吧......怎么样:在PHP中将数组作为参数传递,而不是数组

#6


0  

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

如果你可以改变foo()的代码,应该很容易在一个地方解决这个问题。

function foo()
{
    $args = func_get_args();
    if(count($args) == 1 && is_array($args[0]))
    {
        $args = $args[0]
    }
    // use $args as normal
}

#7


0  

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

根本不建议使用此解决方案,只是展示了一种可能性:

Using eval

使用eval

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

eval(“foo('”。implode(“','”,$ args_array)。“')”);

#1


17  

Use

使用

or

要么

#2


6  

Well you need call_user_func_array

那你需要call_user_func_array

call_user_func_array('foo', $my_args);

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

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

#3


3  

You are searching for call_user_func_array().

您正在搜索call_user_func_array()。

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

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");

#4


2  

Sounds to me like you are looking for call_user_func_array.

听起来像你正在寻找call_user_func_array。

#5


0  

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

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

编辑啊......好吧......怎么样:在PHP中将数组作为参数传递,而不是数组

#6


0  

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

如果你可以改变foo()的代码,应该很容易在一个地方解决这个问题。

function foo()
{
    $args = func_get_args();
    if(count($args) == 1 && is_array($args[0]))
    {
        $args = $args[0]
    }
    // use $args as normal
}

#7


0  

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

根本不建议使用此解决方案,只是展示了一种可能性:

Using eval

使用eval

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

eval(“foo('”。implode(“','”,$ args_array)。“')”);