Forum Moderators: coopster
function foo($variable, ...$array) {
foreach ($array as $key)
echo "$key\n";
}
foo('lorem', 'ipsum', 'quia', 'dolor');
// Result:
// ipsum
// quia
// dolor function foo($variable, $array) {
foreach ($array as $key => $val)
echo "$val\n";
}
foo('lorem', ['one' => 'ipsum', 'two' => 'quia', 'three' => 'dolor']); but is there a way to do it WITHOUT declaring it as an array (eg, wrapping it in [ ]) when I call the function?
function foo($variable,...$array)
{
$n=count($array);
for($i=0;$i<$n;$i+=2)
{
echo "Key: " , $array[$i] , " - " , "Value: " , $array[$i+1] , "\n";
}
}
foo('lorem','one','ipsum', 'two','quia','three','dolor');
Key: one - Value: ipsum
Key: two - Value: quia
Key: three - Value: dolor