Jump to content

passing varying number of arguments by reference


jhecht

Recommended Posts

Well, looked just about everywhere i could, googled, yahoo'd, still cannot figure out an answer.I am making a function, where i need to pass EVERY argument by reference. That itself is not a problem, HOWEVER, the function takes varying numbers of arguments.here's an example:

function thing(){ $args = func_get_args(); for($i=0;$i<sizeof($args);$i++){   $arg = $args[$i];   foreach($arg as $key=>$value){	 if($value=='something'){	   $arg[$key]='else';	 }   } }}$a = array('bob'=>'not something');$b= array('bill'=>'something');thing($a,$b);$c = array('b'=>'something');thing($c);print_r($a);echo "<br />";print_r($b);ehco "<br />";print_r($c);echo "<br />";

The output i would hope from the above would be something along the lines of:Array( [bob]=>not something)Array( [bill]=>else)Array( =>else)I'm just not sure how to go about getting all function arguments by reference, if its possible at all.

Link to comment
Share on other sites

You've probably thought of this, but I'll give it a shot anyway... Could you pass a reference to an array of arguments? Those arguments might also be passed by reference to the array construct/method/thing.

Link to comment
Share on other sites

I could, but in the end that wouldn't do what i need for this to do. Essentially i want for the function to parse an array for values, and alter the arrays given. Obvioulsy i could simply use a return command and go $go = array('bob'=>'billy');$go = thing($go);But i figure its just easier to cut out the middleman

Link to comment
Share on other sites

You could also use indirection and pass the variable names as strings... not sure if this will work though

function thing(){$args = func_get_args();for($i=0;$i<sizeof($args);$i++){   $arg = $args[$i];   global $$arg;   foreach($$arg as $key=>$value){	 if($value=='something'){	   $$arg[$key]='else';	 }   }}}$a = array('bob'=>'not something');$b= array('bill'=>'something');thing("a","b");$c = array('b'=>'something');thing("c");

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...