Hie everyone am trying to pass the following array:
$cars=array("Volvo","BMW","Toyota");
To the following function
function foo(){
$numargs = func_num_args();
$args = func_get_args();
print_r($args);
}
What I want is to make each element in array to be separated element in $args
at the same time I don't to write the function and parameters like that way
foo($cars[0],$cars[1]);
<?php
for ($i=0; $i<sizeof($array); $i++) {
$c = $array[$i];
foo($c);
}
?>
I'm completely sure what it is you are asking but here would be a simple solution: Change foo to:
function foo(){
$args = func_get_args()[0];
}
$args will then contain the array of element you pass into foo() if you called it like this:
foo($cars);
In case you may be passing more than just the array parameter you would need to loop over the arguments and then could do something with each:
foo(){
$args = func_get_args();
while(!empty($args))
{
//do something with each argument. ie. print out it's value
print_r(array_shift($args));
}
}
Call it again by the same way: foo($cars);
It seems a little bit strange what you are trying to so though as I think you could just write you foo method as:
foo($array){
foreach($array as $array_item)
{
// do something with each item ie. print out it's value
print_r($array_item);
}
}