如何将PHP函数的参数列表转换为关联数组?

时间:2021-12-07 16:39:59

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

我想要将参数转换为一个函数,将键等于参数变量名,值等于参数值的关联数组转换为一个关联数组。

PHP:

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?

我该怎么做呢?

3 个解决方案

#1


10  

The you can do this using compact:

你可以用compact来做:

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

Or, get_defined_vars() will give you an associative array of all the variables defined in that scope, which would work, but I think this might also include $_POST, $_GET, etc...

或者,get_defined_vars()会给你一个在这个范围内定义的所有变量的关联数组,这是可行的,但我认为这可能还包括$_POST、$_GET等。

Otherwise, you can use func_get_args to get a list of all the arguments passed to the function. This is not associative though, since it is only data which is passed (that is, there's no variable names). This also lets you have any number of arguments in your function.

否则,可以使用func_get_args获取传递给函数的所有参数的列表。但这不是关联的,因为它只是传递的数据(也就是说,没有变量名)。这也让你在函数中有任意数量的参数。

Or, just specify one argument, which is an array:

或者,只指定一个参数,即一个数组:

function myFunc($params) {

}

compact() seems to be closest to what you're after though.

不过,compact()似乎与您所追求的最接近。

#2


7  

get_defined_vars() is what you need if no other vars are defined before its called inside the function (you can ensure this by making it the first thing you do inside the function).

get_defined_vars()是您需要的,如果没有其他的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)
}

#3


1  

I upvoted @nickf's answer but would like to add that that compact() is also a great way to instantiate a model using a ctor:

我赞成@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;
    }
}

Just make sure that the params have the exact same spelling as the fields.

只需确保params具有与字段相同的拼写。

#1


10  

The you can do this using compact:

你可以用compact来做:

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

Or, get_defined_vars() will give you an associative array of all the variables defined in that scope, which would work, but I think this might also include $_POST, $_GET, etc...

或者,get_defined_vars()会给你一个在这个范围内定义的所有变量的关联数组,这是可行的,但我认为这可能还包括$_POST、$_GET等。

Otherwise, you can use func_get_args to get a list of all the arguments passed to the function. This is not associative though, since it is only data which is passed (that is, there's no variable names). This also lets you have any number of arguments in your function.

否则,可以使用func_get_args获取传递给函数的所有参数的列表。但这不是关联的,因为它只是传递的数据(也就是说,没有变量名)。这也让你在函数中有任意数量的参数。

Or, just specify one argument, which is an array:

或者,只指定一个参数,即一个数组:

function myFunc($params) {

}

compact() seems to be closest to what you're after though.

不过,compact()似乎与您所追求的最接近。

#2


7  

get_defined_vars() is what you need if no other vars are defined before its called inside the function (you can ensure this by making it the first thing you do inside the function).

get_defined_vars()是您需要的,如果没有其他的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)
}

#3


1  

I upvoted @nickf's answer but would like to add that that compact() is also a great way to instantiate a model using a ctor:

我赞成@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;
    }
}

Just make sure that the params have the exact same spelling as the fields.

只需确保params具有与字段相同的拼写。