Reputation: 8378
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
Upvotes: 1
Views: 88
Reputation: 2309
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"
}
Upvotes: 1
Reputation: 8033
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);
Upvotes: 0