0
$data = array (                 
            'id'    => $_POST["id"],
            'name'  => $_POST["name"],
            'email' => $_POST["email"]
            );

Is there a single php function that can transform the array keys into variables having the value of the array value? For instance instead of echoing a value via echo $data['name'] I can simply use echo $name.

Please do not suggest solutions using foreach loops (if possible).

0

4 Answers 4

1

extract does just this: (docs)

 $size = "large";   
 $var_array = array("color" => "blue",
                       "size"  => "medium",
                       "shape" => "sphere");
 extract($var_array, EXTR_PREFIX_SAME, "wddx");

 echo "$color, $size, $shape, $wddx_size\n";

list does as well, in a more controlled fashion (docs), just note that your source array must be numerically indexed - no associative arrays allowed here:

    $info = array('coffee', 'brown', 'caffeine');

    // Listing all the variables
    list($drink, $color, $power) = $info;
    echo "$drink is $color and $power makes it special.\n";
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! extract() seems to work. However I'm not sure if list() works with associative arrays.
You are correct sir, list is for numerically indexed arrays only. I will edit my answer to indicate that fact.
1
extract($_POST);

http://www.php.net/manual/en/function.extract.php

3 Comments

Please, please, please - do not run extract() directly on $_POST/$_GET/$_REQUEST
Agreed! Extract is already a kind of frustrating function for "Johnny Nextguy", running it on $_POST is a headache and a half to sort out.
Yep, I'd have to agree, it's not a great idea to do, but you can use it on any associative array
1

http://php.net/manual/fr/function.list.php

You're maybe looking for the list function, so you know which variable you are dealing with.

list($var, $var2) = $_POST;

1 Comment

As the OP pointed out, list is only valid for numerically indexed arrays.
1

A foreach loop in this case is actually a pretty compact solution...

foreach ( $data as $key => $value ) { $$key = $value; }

Although, simply referencing the array by key is fairly tidy in and of itself...

echo $data['id'];

8 Comments

Why duplicate the functionality of extract with a foreach loop, and how is that compact considering there is a single function call available to accomplish the same result? Normally I wouldn't downvote over this kind of inefficiency, but in this case you came 10 minutes after extract was given as an answer (by several people) to suggest longer code that does the exact same thing... pointless.
You misunderstood then. Security issues with extract() are of the same sort that running PHP with register_globals = On option has. That's why in my comment above, I warned against running extract() directly on GPC arrays. However, if you learn how to use extra arguments extract() takes (especially the prefix one) you can feel perfectly safe.
@CyberJunkie: validation is not sanitation. In general make sure, that extract() will not overwrite any of your variables, that you would not like it to (like $loggedAsAdmin or something ;) ). With the rest deal like you would with any data that comes from userland.
Yes, you're safe using it on an array you made yourself. Just be sure to add comments, don't use extracted values across included files, and don't for Pete's sake make them global. Look - there's a lot of things people will tell you "NEVER DO XXX" or "XXX is harmful". "Bad practice", they scream. This is all silly. If there's a feature in a language, there is an appropriate time and way to use it. Some features, like extract, must be used with caution because they are easy to misuse. That doesn't mean they have the plague. If they were so bad as all that, they wouldn't be in the language.
Even eval() is not evil sometimes ;P
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.