Click to See Complete Forum and Search --> : passing parameters from an array


kydo76
06-10-2004, 01:06 AM
is it possible?

basically... i wanted to loop through an array... each value being passed from the array to being a parameter of a function, without having to assign each value to a variable (i won't know exactly how many parameters will be passed)...

i tried doing it through a loop and passing it as a string... obviously neither way worked... any other suggestions?

thanx

solavar
06-10-2004, 02:42 AM
Yes, it is possible.

Use foreach().


Sorry I can't be more specific than that in the absence of any code.

kydo76
06-10-2004, 05:28 AM
hehe... guess i didnt' explain myself too well....

ok... let's say i have 2 arrays:
$array1=array('function'=>'function1',
'parameters'=>array('parameter1','parameter2'));
$array2=array('function'=>'function2'
'parameters'=>array('parameter1','parameter2','parameter3','parameter4'));

i wanna be able to call them so that it would look like:

function1("parameter1","parameter2");
function2("parameter1","parameter2","parameter3","parameter4")

from a funciton that looks like(sorry for mistakes... i just quickly typed this up... left the original at home.. hopefully you'll get the point anyway.. =)):

function callfunction($array)
{
if (array_key_exists("function",$array))
{
$functionname = $array['function'];
if (array_key_exists("parameters",$array))
{
$parameterarray = $array['parameters'];
$comma_sep_params = implode(",",$parameterarray);
$functionname($comma_sep_params);
}
}
}

obviously it didtn' work .... it would just read $comma_sep_params as one long string... whereas function1 and function2 need 2 and 4 parameters respectively...