开发者

Passing array to function

function a($function, $array)
{
    global $tes开发者_运维百科t

    $test->$function(implode(',' $array));
}

For example, I want to be able to pass the various arguments to a second function inside.

So if I passed a('x', array('a', 'b')) it'd execute $test->x('a', 'b');

The imploding obviously doesn't work due to making it a string, not passing arguments, unsure how to do it.


You could use call_user_func_array().

call_user_func_array(array($test, $function), $array);


function a($function, $array)
{
    global $test

    $test->{$function}($array[0], $array[1]);
}

or

function a($function, $arg1, $arg, $arg3...)
{
    global $test
    $arg = func_get_args();
    unset($arg[0]); // because it is the $function arg
    $test->{$function}($arg);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜