Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php variables to array - opposite of "extract"

PHP has a function extract that will convert an array like this:

$array = array(
 'var1' => 1,
 'var2' => 2
);

to:

$var1 = 1;
$var2 = 2;

now, I need the opposite, i have few variables:

$var3 = 'test';
$test = 'another';
$datax = 1;

that needs to be:

$array = array(
 'var3' => 'test',
 'test' => 'another',
 'datax' => 1
);

Is there something like this in PHP?

like image 698
EscoMaji Avatar asked Feb 21 '12 14:02

EscoMaji


1 Answers

You can use compact() to achieve this.

$var3 = 'test';
$test = 'another';
$datax = 1;
$array = compact('var3', 'test', 'datax');

Reference: http://php.net/manual/en/function.compact.php

like image 89
Marcin Necsord Szulc Avatar answered Oct 08 '22 02:10

Marcin Necsord Szulc