Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7717631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:57:01+00:00 2026-06-01T02:57:01+00:00

One thing I find myself often wanting to do is convert a large associative

  • 0

One thing I find myself often wanting to do is convert a large associative array, usually from a POST request (a registration form, for example, with many inputs) into local variables based on the key from the array. If you are using the values from the array often, your code quickly becomes littered with long variable names and quotes.

To prevent this for small associative arrays, it is acceptable to just do something like this:

$username = $_POST['username'];
$password = $_POST['password'];

I’m not escaping them for this example to keep everything as uncluttered as possible, so relax.

You can also do this:

list($username, $password) = $_POST;

But what if the $_POST array is larger? Then it becomes tedious to do both of those methods. To fix that, you can run a loop like this:

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

The problem with this method is that it assigns local variables. It would be nice if it were possible to call a function in a parent class that runs this loop and those local variables were accessible within the calling class. Imagine an MVC setup where every controller is derived from a Controller class, and likewise for the models. It would be nice to do:

$this->localize($_POST);
doWhatever($username) // $username works! Assuming $_POST['username'] is defined

Creating such a method causes the local variables to only remain within the scope of the localize() function in the parent class, so this won’t work. What I have been doing is running the same loop with one modification:

foreach($arr as $key => $value)
{
    $this->{$key} = $value;
}

This works and all, but doesn’t really solve the initial problem. Instead of code that is cluttered with brackets and quotes, it has $this-> all over the place, not to mention it is assigning variables that were never formally defined within the class.

So finally, my question: is it possible to create a function such as the localize() I described, such that it can be inherited from a parent class but create local variables relative to the child class ($username instead of $this->username).

Also, whether you can or not, is this considered bad practice? It seems a bit hacky to me, and you are ignoring some principles of OOP. If it is, do you use a solution to fix the ugliness and clutter of large associative arrays, or do you just deal with it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:57:03+00:00Added an answer on June 1, 2026 at 2:57 am

    The PHP extract function does exactly this. Extracts a hash into the local namespace:

    https://www.php.net/extract

    An important addendum:

    Be sure to use the EXTR_SKIP option for extract() if the function is acting on user-supplied variable names (such as provided by $_POST) to prevent redefining of existing variables:

    $myvar = 'abc';
    extract($_POST, EXTR_SKIP);  // $_POST['myvar'] won't overwrite $myvar.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One of the things I find myself doing often is passing string literals as
I'm using Google+ quite heavily and one thing I find myself doing extremely frequently
I often find myself with a file that has one number per line. I
One thing really really annoys me about the Eclipse ide : its find UI
One thing I've started doing more often recently is retrieving some data at the
I use MySQLdb a lot when dealing with my webserver. I often find myself
I often find these three variants: SELECT COUNT(*) FROM Foo; SELECT COUNT(1) FROM Foo;
often I find myself suprized about simple things, I'm not sure but I believe
Often I find myself in a situation where I have to deal with catching
The more I get into writing unit tests the more often I find myself

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.