Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm looking for something like the opposite of extract(), and I know its NOT compact().

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" );
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
 
I'm not sure if your correct or not.. but you do realize he said explicitly in the question that its NOT compact() right? –  Greg Guida Nov 16 '11 at 4:14
 
@Greg: Yes, I forgot to include my explanation... –  animuson Nov 16 '11 at 4:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.