PHP get all function arguments as $key => $value array?
Question by web lover
<?php
function register_template(){
print_r(func_get_args());
# the result was an array ( [0] => my template [1] => screenshot.png [2] => nice template .. )
}
register_template( # unkown number of arguments
$name = "my template",
$screenshot = "screenshot.png",
$description = "nice template .. "
)
?>
BUT , I want the result array as $key => $value form , $key represents the parameter name.
Answer by Starx
It’s easy. Just pass the array as the parameter instead then later access it as $key => $value
inside the function.
UPDATE
This was the best I could think of
$vars = array("var1","var2"); //define the variable one extra time here
$$vars[0] = 'value1'; // or use $var1
$$vars[1] = 'value2'; // or use $var2
function myfunction() {
global $vars;
$fVars = func_get_args();
foreach($fVars as $key=>$value) {
$fvars[$vars[$key]] = $value;
unset($fvar[$key]);
}
//now you have what you want var1=> value1
}
myfunction(array($$vars[0],$$vars[1]));
I haven’t tested it…BTW. But you should get the point