3

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!

2 Answers 2

12

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...

Sign up to request clarification or add additional context in comments.

3 Comments

Awesome. Didn't even know how I would search for this function.
@stereo bit more explanation would be awesome, under what circumstances?
Sure the explanation could be added. But the key purpose was to provide a PHP function that could do the job and I've mentioned the link to the PHP manual that explains it nicely; I left the implementation totally up to the implementer. I don't think using extract() is a bad practice, it's just you need to be sure what you are extracting. And then there are various extract types.
-1
$array = array(
    'myVar1' => 'value1',
    'myVar2' => 'value1',
    'myVar3' => 'value3',
);

echo $array['myVar1'];
echo $array['myVar2'];

Is that what you mean?

1 Comment

I think he means $myVar1, so he doesn't have to use $array['myVar1']

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.