8

I'm looking for something like the opposite of extract().

Say I have a few variables

$state = "FL";
$city = "Hollywood";
$zip = "33021";

How can I make an array that uses the variables' names as the array keys:

array( "state"=>"FL", "city"=>"Hollywood", "zip"=>"33021" );
0

1 Answer 1

9

You should use the compact() function though. Such as:

$state = "FL";
$city = "Hollywood";
$zip = "33021";
$array = compact('state', 'city', 'zip');

Edit: Seems to do exactly what you need, don't know why you don't think you need it. If you're looking for something exactly opposite of extract(), such as taking all available variables and putting them into an array, you can't do that, because PHP would literally take all variables in the current scope and put them into the array. You have to specify which variables somehow.

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

3 Comments

I'm not sure if your correct or not.. but you do realize he said explicitly in the question that its NOT compact() right?
compact() has a downside, in that most IDEs won't realize there is a hidden reference to those variables.
Yeah, compact() is the answer but please avoid it to avoid confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.