Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a PHP function's parameter list to an associative array?

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:

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?

like image 581
Fragsworth Avatar asked Sep 14 '09 02:09

Fragsworth


People also ask

How do I get associative array in PHP?

Answer: Use the PHP array_values() function You can use the PHP array_values() function to get all the values of an associative array.

Is $_ POST an associative array?

$_POST is a predefined variable which is an associative array of key-value pairs passed to a URL by HTTP POST method that uses URLEncoded or multipart/form-data content-type in request.

Which associative array is used to pass data to PHP?

The $_POST is an associative array of variables. These variables can be passed by using a web form using the post method or it can be an application that sends data by HTTP-Content type in the request.

What is associative arrays in PHP give some example?

Associative Array - It refers to an array with strings as an index. Rather than storing element values in a strict linear index order, this stores them in combination with key values. Multiple indices are used to access values in a multidimensional array, which contains one or more arrays.


2 Answers

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:

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.

like image 133
StartupGuy Avatar answered Sep 24 '22 00:09

StartupGuy


The you can do this using 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...

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.

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

function myFunc($params) {

}

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

like image 33
nickf Avatar answered Sep 24 '22 00:09

nickf