Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create array with key name same as $variable name assigning to array?

I am assigning an array with the $variables(have different name) that have some values like:

array($one, $two, $three);

now i am a searching a way that is there any way to assign this above array with key name same as variable name directly, finally i want to make this array like:

array(
      "one" => $one, 
      "two" => $two, 
      "three" => $three
)

if this can possible at the time of assigning array with the variable then i would be good for me then i can easily extract() this array and i don't have to do operation to open this array and assigning these values with the $variable name, if any function() exist for that then it will be good, please help if any one knows

like image 824
Vishal Solanki Avatar asked Oct 28 '25 05:10

Vishal Solanki


1 Answers

compact is the inverse of extract. Use it like this :

$array = compact ('one', 'two', 'three');

This will look for the variables named 'one', 'two', 'three', and does exactly what you are looking for.

like image 177
Lorenz Meyer Avatar answered Oct 29 '25 20:10

Lorenz Meyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!