【发布时间】:2010-11-30 20:11:33
【问题描述】:
我想在函数调用中使用数组中的值作为独立参数。示例:
// 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}"; }
数组中值的个数未知。
可能的解决方案:
我可以将数组作为单个参数传递 - 但更愿意作为多个独立的函数参数传递。
implode()将数组转换为逗号分隔的字符串。 (失败,因为它只是一个字符串。)-
使用单个参数:
$str = "'a','b'"; function goat($str); // $str needs to be parsed as two independent values/variables. 使用
eval()?遍历数组?
欢迎提出建议。谢谢。
【问题讨论】:
-
"数组中值的个数未知。" -- 它们是否至少与函数签名的 arg 列表相匹配?