How to convert a list of PHP function parameters to an associative array?

I want to convert the arguments to a function into an associative array with keys equal to the names of the variable parameters and values ​​equal to the values ​​of the parameters.

PHP:

function my_function($a, $b, $c) {

    // <--- magic goes here to create the $params array

    var_dump($params['a'] === $a); // Should result in bool(true)
    var_dump($params['b'] === $b); // Should result in bool(true)
    var_dump($params['c'] === $c); // Should result in bool(true)
}

How can i do this?

+7
source share
4 answers

You can do this using compact:

function myFunc($a, $b, $c) {
    $params = compact('a', 'b', 'c');
    // ...
}

Or, it get_defined_vars()will give you an associative array of all the variables defined in this area that will work, but I think it may also include $_POST, $_GETetc.

func_get_args, , . , (.. ). .

, :

function myFunc($params) {

}

compact() , .

+11

get_defined_vars() - , , ( , , ).

function my_function($a, $b, $c) {

    $params = get_defined_vars(); // <--- create the $params array

    var_dump($params['a'] === $a); // results in bool(true)
    var_dump($params['b'] === $b); // results in bool(true)
    var_dump($params['c'] === $c); // results in bool(true)
}
+9

@nickf, , compact() ctor:

class User {
    public $email;
    public $password;
    public $firstName;
    public $lastName;

    public function __construct ( $email, $password, $firstName, $lastName )
    {
        foreach ( compact( array_keys( (array)$this )) as $k => $v )
            $this->$k = $v;
    }
}

, , .

+2

As suggested, but not implemented: you can use to collect parameter names, and then associate them with values: Reflection

function args_to_assoc_array($function, $args) {                                 
    if (false === strpos($function, '::')) {                                     
        $reflection = new ReflectionFunction($function);                         
    } else {                                                                     
        $reflection = new ReflectionMethod(...explode('::', $function));         
    }                                                                            
    $assoc = [];                                                                 
    foreach ($reflection->getParameters() as $i => $parameter) {                 
        $assoc[$parameter->getName()] = $args[$i];                               
    }                                                                            
    return $assoc;                                                               
}                                                                                

You can then call it the same way, whether in a function or method:

function f($x, $y) { return args_to_assoc_array(__METHOD__, func_get_args()); }
class A {                                                                     
    function m($z) { return args_to_assoc_array(__METHOD__, func_get_args()); }
}                                                                             
var_dump(f(7, 2), (new A)->m(4));                                             

what conclusions :

array(2) {
  ["x"]=>
  int(7)
  ["y"]=>
  int(2)
}
array(1) {
  ["z"]=>
  int(4)
}

I did not count this, but I strongly suspect that it is much slower than using compact.

0
source

All Articles