PHP gets all function arguments as $ key => $ value array?

<?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 an array of results in the form $ key => $ value form, $ key represents the parameter name.

+5
source share
11 answers

PHP does not support an arbitrary number of named parameters. You either define a fixed number of parameters and their names in the function declaration, or you get only values.

The usual way to use an array is:

 function register_template($args) { // use $args } register_template(array('name' => 'my template', ...)); 
+13
source

I wanted to do the same and was not completely satisfied with the answers that have already been given ....

Try adding this to your function →

 $reflector = new ReflectionClass(__CLASS__); $parameters = $reflector->getMethod(__FUNCTION__)->getParameters(); $args = array(); foreach($parameters as $parameter) { $args[$parameter->name] = ${$parameter->name}; } print_r($args); 

I did not think about trying to make this my own function, but you can just call, but maybe you can ...

+12
source

There is no such thing as a parameter name. frobnicate($a = "b") not the syntax for calling with a parameter, but simply an assignment, followed by a function call - a trick used to document the code, not actually taken into account by the language.

It is usually customary to provide an associative array of parameters in the form: frobnicate(array('a' => 'b'))

+3
source

Option A)

 <?php function registerTemplateA() { // loop over every variable defined in the global scope, // such as those you created there when calling this function foreach($GLOBALS as $potentialKey => $potentialValue) { $valueArgs = func_get_args(); if (in_array($potentialValue, $valueArgs)) { // this variable seems to match a _value_ you passed in $args[$potentialKey] = $potentialValue; } } // you now have an associative array in $args print_r($args); } registerTemplateA($name = "my template", $screenshot = "screenshot.png", $description = "nice template"); ?> 

Option B)

 <?php function registerTemplateB() { // passing in keys as args this time so we don't need to access global scope for ($i = 0; $i < func_num_args(); $i++) { // run following code on even args // (the even args are numbered as odd since it counts from zero) // `% 2` is a modulus operation (calculating remainder when dividing by 2) if ($i % 2 != 0) { $key = func_get_arg($i - 1); $value = func_get_arg($i); // join odd and even args together as key/value pairs $args[$key] = $value; } } // you now have an associative array in $args print_r($args); } registerTemplateB('name', 'my template', 'screenshot', 'screenshot.png', 'description', 'nice template'); ?> 

Option C)

 <?php function registerTemplateC($args) { // you now have an associative array in $args print_r($args); } registerTemplateC(array('name' => 'my template', 'screenshot' => 'screenshot.png', 'description' => 'nice template')); ?> 

Conclusion: option C is the best "for minimal code"

(Note: this answer is valid PHP code with open and closing tags in the right places, verified using PHP 5.2.x and should also work on PHP 4 ... so try it if you need to.)

+2
source

It is easy. Just pass the array as a parameter, and then access it as $key => $value inside the function.

UPDATE

That was the best I could come up with.

 $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 have not tested it ... BTW. But you must get a point

+1
source

GET FUNCTION PARAMETERS AS A NAME MAP => VALUE

 function test($a,$b,$c){ // result map nameParam=>value $inMapParameters=[]; //get values of parameters $fnValueParameters=func_get_args(); $method = new \ReflectionMethod($this, 'test'); foreach ($method->getParameters() as $index=>$param) { $name = $param->getName(); if($fnValueParameters[$index]==null){continue;} $inMapParameters["$name"]=$fnValueParameters[$index]; } } 
+1
source
 class MyAwesomeClass { public static function apple($kind, $weight, $color = 'green') { $args = self::funcGetNamedParams(); print_r($args); } private static function funcGetNamedParams() { $func = debug_backtrace()[1]['function']; $args = debug_backtrace()[1]['args']; $reflector = new \ReflectionClass(__CLASS__); $params = []; foreach($reflector->getMethod($func)->getParameters() as $k => $parameter){ $params[$parameter->name] = isset($args[$k]) ? $args[$k] : $parameter->getDefaultValue(); } return $params; } } 

Let him test it!

 MyAwesomeClass::apple('Granny Smith', 250); 

Output:

 Array ( [kind] => Granny Smith [weight] => 250 [color] => green ) 
+1
source

you cannot, arguments are only positional. maybe you can send an array?

 <?php function register_template(array $parameters) { var_dump($parameters); } register_template(# unkown number of arguments $name = "my template", $screenshot = "screenshot.png", $description = "nice template .. " ) ?> 
0
source

Use

 function register_template($args){ print_r ( $args ); // array (['name'] => 'my template' ... extract ($args); print $name; // my template print $screenshot; } register_templete ( array ( "name" => "my template", "screenshot" => "screenshot.png", "description" => "nice template.." )); 
0
source

You can do this via get_defined_vars (), but you should not forget that this function returns every variable in the scope of the function.

For instance:

 <?php function asd($q, $w, $rer){ $test = '23ew'; var_dump(get_defined_vars()); } asd("qweqwe", 'ewrq', '43ewdsa'); ?> 

Return:

 array(4) { ["q"]=> string(6) "qweqwe" ["w"]=> string(4) "ewrq" ["rer"]=> string(7) "43ewdsa" ["test"]=> string(4) "23ew" } 
0
source

use this:

 foreach(func_get_args() as $k => $v) echo $k . " => " . $v . "<BR/>"; 
-3
source

Source: https://habr.com/ru/post/1335375/


All Articles