开发者

Variables into array - $array[$var_name] = $var;

开发者 https://www.devze.com 2023-04-06 10:13 出处:网络
I was wondering whether something like this is possible: $var1 = 1; $var2 = 2; function varsToArray($param1, $param2...);

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消