I want to use values in an array as independent arguments in a function call. Example:
// Values "a" and "b"
$arr = array("alpha", "beta");
// ... are to be inserted as $a and $b.
my_func($a, $b)
function my_func($a,$b=NULL) { echo "{$a} - {$b}"; }
The number of values in the array are unknown.
Possible solutions:
I can pass the array as a single argument - but would prefer to pass as multiple, independent function arguments.
implode()
the array into a comma-separated string. (Fails because it's just one string.)
Using a single parameter:
$str = "'a','b'";
function goat($str); // $str needs to be parsed as two independent values/variables.
Use eval()
?
Traverse the array?
Suggestions are appreciated. Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…