admin管理员组

文章数量:1200788

In JavaScript, I can use apply to pass an array as arguments to a function:

var f = function (n,m) {},
    args = [1,2];

f.apply(null, args);

I now need to do something similar in PHP i.e. pass an array of items as 'separate' arguments to a function.

Is there any way this can be done?

In JavaScript, I can use apply to pass an array as arguments to a function:

var f = function (n,m) {},
    args = [1,2];

f.apply(null, args);

I now need to do something similar in PHP i.e. pass an array of items as 'separate' arguments to a function.

Is there any way this can be done?

Share Improve this question asked Aug 14, 2011 at 22:51 Andreas GrechAndreas Grech 108k101 gold badges303 silver badges362 bronze badges 6
  • you can just parse the array as the argument – user557846 Commented Aug 14, 2011 at 23:01
  • Sure, but it's much cleaner to pass them as separate arguments. – Andreas Grech Commented Aug 14, 2011 at 23:07
  • is it? don't see why, there's a place for both i guess. – user557846 Commented Aug 14, 2011 at 23:12
  • 4 Dagon: having worked on a big codebase that uses arrays pretty much everywhere for arguments, I can tell you it's a LOT easier when it's simple arguments. It's self documenting for one thing.. – Evert Commented Aug 15, 2011 at 1:26
  • 1 @NickyDeMaeyer even more work to worry about 2 year old threads :-) – user557846 Commented Jul 4, 2013 at 8:59
 |  Show 1 more comment

1 Answer 1

Reset to default 25

You can use the function call_user_func_array. Simply pass in your function (as a callback, usually a string with the function name), and an array of arguments.

Additional note: for static functions, use forward_static_call_array.

本文标签: Is there something like JavaScript39s apply function in PHPStack Overflow