Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting variables from a Key Value Array PHP

Tags:

arrays

php

I have an array:

array(
    'myVar1' => 'value1',
    'myVar2' => 'value1',
    'myVar3' => 'value3',
);

Is there a built in function in PHP that will make 3 variables e.g. $myVar1, $myVar2, $myVar3 do that when i echo $myVar1; it retuens 'value1'

Obviously I can loop the array and set them accordingly (so please no answers with this), but if there is a internal PHP function that would be great!

like image 255
Lizard Avatar asked Jun 12 '26 12:06

Lizard


1 Answers

extract() is the function:

Import variables into the current symbol table from an array...

Checks each key to see whether it has a valid variable name. It also checks for collisions with existing variables in the symbol table...

like image 136
Abhay Avatar answered Jun 15 '26 02:06

Abhay



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!