1

I want to use array as an argument in my function and then convert/extract that array and apply each element into another function's argument after the first argument.

$args = [ $arg1, $arg2, ...];

function foo($one = 'string', $two='string', array $args = [])
{

    // my stuffs

    //here calling another function where need to pass arguments
    // the first argument is required
    // from second argument I want to apply array item individually.
    // So this function has func_get_args()
    call_another_function($one, $arg1, $arg2, ...);

}

So how I can convert my function array items and apply each item to call_another_function from second parameters to infinity based on array items

5
  • So what's your question? Commented Oct 24, 2015 at 4:42
  • @JohnConde how I can convert my function array items and apply each item to call_another_function from second parameters to infinity based on array items Commented Oct 24, 2015 at 4:44
  • 2
    Whats about the php build in function call_user_func_array? php.net/manual/de/function.call-user-func-array.php Commented Oct 24, 2015 at 4:52
  • call_another_funciton` is already using that to get all function arguments. Here first parameter has been sliced in that funciton using array_slice Commented Oct 24, 2015 at 4:56
  • 1
    @CodeLover call_user_func_array calls a function with the arguments as array. I will give you an example based on your code. Commented Oct 24, 2015 at 5:16

2 Answers 2

1

As mentioned in the comment, you can use call_user_func_array to call the call_another_function function:

<?php
$args = [ $arg1, $arg2, ...];

function foo($one = 'string', $two='string', array $args = [])
{

    // your stuffs

    // Put the first argument to the beginning of $args
    array_unshift($args, $one);
    // Call another function with the new arguments
    call_user_func_array('call_another_function', $args);

}

function call_another_function(){
    var_dump(func_get_args());
}

foo('one', 'two', $args);

this will output something like that:

array(3) {
  [0] =>
  string(3) "one"
  [1] =>
  string(4) "arg1"
  [2] =>
  string(4) "arg2"
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think the call_another_function should have this signiture:

function call_another_function($one, $argumentsArray)

The second parameters is an array and you can add any number of argument to it.

$argumentArray = array();
array_push($argumentArray, "value1");
array_push($argumentArray, "value2");
array_push($argumentArray, "value3");
...
call_another_function($one, $argumentArray);

1 Comment

Thanks for the code but I am failed to understand it. The call_another_function() has $funcargs=func_get_args();` in it.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.