php变量到数组 - 与"extract"相反

Esc*_*aji 8 php arrays extract

PHP有一个函数提取,将像这样转换一个数组:

$array = array(
 'var1' => 1,
 'var2' => 2
);
Run Code Online (Sandbox Code Playgroud)

至:

$var1 = 1;
$var2 = 2;
Run Code Online (Sandbox Code Playgroud)

现在,我需要相反的,我有几个变量:

$var3 = 'test';
$test = 'another';
$datax = 1;
Run Code Online (Sandbox Code Playgroud)

需要是:

$array = array(
 'var3' => 'test',
 'test' => 'another',
 'datax' => 1
);
Run Code Online (Sandbox Code Playgroud)

在PHP中有这样的东西吗?

Mar*_*ulc 21

你可以用它compact()来实现这一目标.

$var3 = 'test';
$test = 'another';
$datax = 1;
$array = compact('var3', 'test', 'datax');
Run Code Online (Sandbox Code Playgroud)

参考:http://php.net/manual/en/function.compact.php