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 4615820
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:53:40+00:00 2026-05-22T01:53:40+00:00

I have several variables coming from an array in $POST_[‘array’] i wish to make

  • 0

I have several variables coming from an array in $POST_[‘array’] i wish to make some kind of loop for example foreach that makes, for every value in the variable a variable name of it and assigns the value for it.

For example if i have

$POST_['name'];
$POST_['last'];
$POST_['age'];
$POST_['sex'];

I want the loop to create each variable from the array inside the $_POST with the name of the variable like the following:

$name = 'John';
$last = 'Doe';
$age = '32';
$sex = 'male';

NOTE – The array is coming from a serialized jquery string that puts together all the variables and values in a form into one big string.

Is this possible?

  • 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-05-22T01:53:41+00:00Added an answer on May 22, 2026 at 1:53 am

    You don’t need a loop, you want extract:

    extract($_POST); // But use caution, see below
    

    ##Cautions and best practices

    As noted in the comments this forces all parameters in the $_POST array into the current symbol space.

    In global space

    <?php
    extract($_GET);
    var_dump($_SERVER); // Can be overwritten by the GET param
    ?>
    

    The code above illustrates the problem as shown in this answer — some pretty dangerous things can be overwritten in the global space.

    Inside a function

    function myFunc() {
        // (Mostly) empty symbol space! (excluding super globals)
        extract($_POST);
    }
    

    Inside a function, as the first line, no harm done.

    Important note: You might think since $_SERVER is a super global, that this exploit could happen inside a function as well. However, in my testing, on PHP Version 5.3.4, it is safe inside a function — neither $_SERVER, $_POST, $_GET, $_SESSION, or presumably other superglobals, could be overwritten.

    With options

    You can also use extract with extract_type options that do not overwrite.

    The best option to use, in my opinion, is simply to prefix all variables from extract:

    // $_GET = test=1&name=Joe
    
    extract($_GET, EXTR_PREFIX_ALL, "request_get");
    
    echo $request_get_test; // 1
    echo $request_get_name; // Joe
    

    That way you don’t have the overwrite problem, but you also know you got everything from the array.

    ##Alternate – looping w/ conditional

    If you wanted to do this manually (but still dynamically), or wanted to conditionally extract only a few of the variables, you can use variable variables:

    foreach ($_POST as $key => $value) {
        if (isset($$key)) continue;
    
        $$key = $value;
    }
    

    (The example condition I’ve used is an overwrite prevention.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several variables of type char (array), int, and double. Is there a
I have needed in several occasions some classes to represent and manipulate conditions (typically
I'm having a problem, where I wish to run several command line functions from
There is a problem with session variables in my web-application. I have several types
Say you have several large arrays of variables (which generally are strings), and you
In my code, I have several pointers to pointers (e.g. float ** variables), and
I have several ASP:TextBox controls on a form (about 20). When the form loads,
I have several RequiredFieldValidators in an ASP.NET 1.1 web application that are firing on
I have several tables whose only unique data is a uniqueidentifier (a Guid) column.
We have several jobs that run concurrently that have to use the same config

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.