Log in

View Full Version : Resolved Passing an array into arguments



fileserverdirect
09-20-2010, 09:57 PM
Yes I googled this, but I could not find anything. Here is my function that is in a class:


public function create()
{
$args = func_get_args();
$element = $args[0];
if(isset($this->elements->$element)) {
$value=$this->elements->$element;
unset($args[0]);
$format = implode(", ", $args);
printf($value, $format);
return true;
}
else {
return false;
}
}

My problem is that I want to replace each argument from the original create() function into printf. $this->elements contains an object with formatted elements, ready for printf. I have gotten this to work with one argument, but some elements require multiple ones. How do I pass an array into arguments that a function will read each as it's own argument?

djr33
09-21-2010, 05:07 AM
I'm not sure I understand the problem.

Can you do a foreach loop with a second function?

func1() {
$a = getargs;
foreach($a as $item) {
func2($item);
}
}

fileserverdirect
09-21-2010, 06:54 PM
I should have been more clear, I want an values in an array to be attributes in a function. Like:

$eg = array("attribute2", "attribute3")
then have each become a actually attribute into a function once:

printf($always-here, "attribute2", "attribute3")
The last two can change dynamicly, depending on how many values are in the array.

fileserverdirect
09-21-2010, 10:19 PM
Ok, I figured it out. For those who want to know:


call_user_func_array($function_name, $attributes_in_an_array);

*Works with built in functions*
http://php.net/manual/en/function.call-user-func-array.php


Apparently there is ANOTHER function called vprintf, which takes an array directly!

djr33
09-22-2010, 12:56 AM
Ah, I see. That can be tough, but it looks like that solution works fine.