Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Convert Key => Value Arrays Into Standard Variables In PHP

10 Responses »

I don't mind using arrays -- in fact, I love them. They're easy to use and hold collections of information well.

I don't always want to use arrays though. Why use:

echo $user['first_name'];

when you could simply use:

echo $first_name;

So how can we get rid of the array part of the variables?

The Code

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

//or....

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

A few variables you must be careful with are:

  • $_GET
  • $_POST
  • $_REQUEST

Why? Because $_GET, $_POST, and $_REQUEST variables can manipulated by the user and that would present a huge security concern.

Discussion

  1. December 7, 2007 @ 9:56 am

    Interesting effect, but it has the same effect of php’s extract function! In your example, you can do extract($user); and so call $first_name; ;)

    http://www.php.net/extract

  2. December 7, 2007 @ 10:22 am

    Good point Hugo. My post wasn’t detailed enough, or perhaps was over-simplified.

    With the loop, you can do special operations to the values. For example:

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

    …removes slashes.

  3. peter scarborough
    December 14, 2007 @ 12:31 pm

    I have used this alot for both GET’s and POST’s, sure, they can create their own variables, who cares? If I use a variable, I always declare it after pulling from get or post, the variables that I use from post and get are also validated for correct values.

    Also, something I have found handy for dealing with case sensitivity for get variables, (since sometimes if someone posts it to say, ICQ for someone else to visit, ICQ will lowercase the whole string) is
    foreach($_GET as $k = $v) {
    $k = strtolower($k);
    \$$k = $v
    }
    then you can safely test to see if the get was set
    if(isset($variablenametotestfor)) {//begin execution}

  4. peter scarborough
    December 14, 2007 @ 1:37 pm

    Also, per using the method for GET POST or REQUEST you could use a regex match on the $k to only convert expected variable names.
    IE:
    $acceptedGETS = /(varname1)|(varname2)|(varname3)|(etc)/
    foreach($_GET as $key => $value) {
    $key = strtolower($key);
    if $key.match($acceptedGETS) $$key = $value;
    }

    This will prevent someone from creating their own variable names

  5. December 14, 2007 @ 1:45 pm

    Thank you for the RegEx, Peter — that could prove very useful!

  6. peter scarborough
    December 14, 2007 @ 3:56 pm

    Sorry, there was an error in my regex syntax there, I learned regex with javascript, and just learning PHP regEx… I got the two confused when I wrote that

    The correct syntax for the above would be
    $acceptedGETS = “/\bvarname1\b|\bvarname2\b|\bvarname3\b|\betc\b/”;
    foreach($_GET as $key => $value) {
    $key = strtolower($key);
    if (preg_match($acceptedGETS,$key)) $$key = $value;
    }

    the \b is required on both sides of each variable name to force exact words, without them qvarname1x34r would match.

  7. February 27, 2010 @ 10:55 pm

    Hey is not on google so I was wondering what the ${ } does? I have never seen it before.

  8. March 4, 2010 @ 9:37 pm

    Hi David,

    I think the brackets are just a way of delimiting the variable’s name from the rest of the string.

    Say you had variable $table_prefix,

    echo “table name: $table_prefix_users”; // would return “table name: “;
    echo “table name: ${table_prefix}_users”; // would return “table name: wp_users”;

    I am not sure about this :) I am most likely wrong.

    Cheers!
    Droope

  9. March 30, 2010 @ 12:18 pm

    No, Droope is correct. Mostly.

    The ${ } does designate a string.

    But the purpose of this article is showing how to easily assign array variables to their key name. I know that sounds confusing, but its it.

    Example:

    $people['me'] = ‘robert’;
    $people['friend'] = ‘sam’;
    $people['girl'] = ‘bailey’;

    foreach($people as $key => $val) {
    // ${“me”} = ‘robert’

    ${$key} = $val; //or $$key = $val;
    }

    So that

    echo $me; //echo “robert”;

  10. April 5, 2010 @ 3:02 pm

    (great blog, btw)

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!