2

I Need to use call_user_func_array, but my array is too big, from the documentation i only got to use array values in the argument list. Is there no way to use the same array as argument of the callback function.

mixed call_user_func_array ( callable $callback , array $param_arr )

My Code:

echo call_user_func_array("myFirstFunction" , array("1" , "2" , "3")); 

function myFirstFunction($arg1, $arg2, $arg3){
    return $arg1 . $arg2 . $arg3;
}

My Question is if i have array with 50 or more values than how can use this function?

2
  • Why do you have to use call_user_func_array? Commented May 30, 2016 at 7:10
  • pass an array to an function. Commented May 30, 2016 at 7:16

2 Answers 2

1

Use func_get_args

echo call_user_func_array("myFirstFunction" , array("1" , "2" , "3")); 

function myFirstFunction(){
    $arguments = func_get_args(); //Now, $arguments is an array, you can process further
    return implode("", $arguments);
}

Result:

123

Demo

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

6 Comments

for php7 see docs.php.net//functions.arguments#functions.variable-arg-list . But if you really want/have to pass 50+ values to a function isn't an array/iterator as parameter better anyway?
Hello sir, would you have any suggestion for this?
Given the information provided, no, not really. Something like codepad.org/ZvsLuB91 , but it depends on what you're actually trying to achieve.
So from your suggestion if i have some big size array than go with foreach... not call_user_func_array.
I those are the alternatives without further information, then: yes (tentative)
|
0

You can also use array_reduce:

<?php

echo array_reduce(['1', '2', '3'], "myFirstFunction");

function myFirstFunction($prev, $arg){
    return $prev . $arg;
}

5 Comments

Do you have some explanation for this?? I really don't know that how this solve my problem.
array_reduce will reduce the array to one single value using the callback function, myFirstFunction.
That is under the assumption that reducing the array fits the OP's needs. From the rather generic question (which triggers at least in me the generic answer "just don't" ;-) ) we don't know what's actually required.
What are $prev, $arg things do here??
Thanks for the help.

Your Answer

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