Variables into array - $array[$var_name] = $var;
I was wondering whether something like this is possible:
$var1 = 1; $var2 = 2;
function varsToArray($param1, $param2...);
and it returns array like this
array([var1] => 1, [var2] => 2).
Simply saying, I'd like ar开发者_StackOverflowrays keys to be same as variable names. The problem is I don't know how to get variable name as string to put it as key(if possible of course...).
You want to use the compact
function methinks.
The built-in PHP function compact()
does this.
BTW: I'm going to go ahead and assume that instead of:
function $varsToArray($var1, $var2...);
you actually meant:
function varsToArray($var1, $var2...);
Notice the dollar sign has been removed.
精彩评论