I want to use the values in the array as independent parameters in the 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 is unknown.
Possible solutions are:
I can pass the array as a single parameter, but would prefer to pass it as multiple independent function parameters.
Concatenate the arrays into comma-separated strings using the implode() function. (Fails because it's just a string.)
Use 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 welcome. Thanks.
If I understand you correctly:
This question is quite old, but has more direct support in PHP 5.6 version.
http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list.new