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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:45:47+00:00 2026-06-03T05:45:47+00:00

I have built an empty associative array whos keynames refer to submitted post data.

  • 0

I have built an empty associative array whos keynames refer to submitted post data. I can capture the postdata just fine, but I run into trouble trying to instantiate variables who’s names match the array key.

for example:

$insArray = array('rUsername'=>'', 'rPass'=>'', 'rQuestion'=>'', 'rAnswer'=>'', 'rFName'=>'', 'rLName'=>'', 'rBDateD'=>'', 'rBDateM'=>'', 'rBDateY'=>'', 'rHCheck'=>'', 'rHCeckOption'=>'', 'rEmail'=>'');

foreach($insArray as $key=>$value){
    if (filter_input(INPUT_POST, $key) != ''){
        $key = stripslashes(filter_input(INPUT_POST, $key)); 
        $insArray[$key] = $key;
    }       
}

First line creates the empty array, then the foreach loops through this array. Now it gets tricky.

filter_input(INPUT_POST, $key) captures the value located in the post data matching the current key, rUsername in this case

$key is where the problem lies. I want the NAME of the new variable to be the associative key name, for example I want to replace $key with $rUsername in the first iteration, $rPass in the second, and so on. I tried using two $$, but I know that’s not right. Never tried doing this before, but it would be helpful if I could figure it out.

UPDATE:

This is the final code which was a combination of two of the answers provided.

if (isset($_POST['submit'])) {
    //Build array of variables to be put into database
    $insArray = array('rUsername'=>'', 'rPassword'=>'', 'rQuestion'=>'', 'rAnswer'=>'', 'rFName'=>'', 'rLName'=>'', 'rBDateD'=>'', 'rBDateM'=>'', 'rBDateY'=>'', 'rHCheck'=>'', 'rHCheckOption'=>'', 'rEmail'=>'');

    foreach(array_keys($insArray) as $key){
        $insArray[$key] = filter_input(INPUT_POST, $key);
        $$key = filter_input(INPUT_POST, $key);
    }
}

Gave me exactly the output I wanted, thanks guys!

  • 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-03T05:45:48+00:00Added an answer on June 3, 2026 at 5:45 am

    You’re not accessing $_POST at all, so all you’re doing is taking some array members you defined yourself, filtering them for harmful POST characters (why would you attempt to inject your own code?) and then creating a new array from those self-defined key values.

    If I’m guessing right at what you want, it should be this:

    foreach(array_keys($insArray) as $key) {
        $insArray[$key] = stripslashes(filter_input(INPUT_POST, $_POST[$key]));
    }
    

    The use of stripslashes suggests that you’re on a braindead version of PHP which has magic_quotes enable. You should upgrade to a modern version of PHP and/or turn them off.

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

Sidebar

Related Questions

I have built a data driven model by dragging my tables onto the empty
I have a form built with jquery. I just want to have an empty
I have a list which is dynamically built, but there are empty list items
I'm just getting my feet wet with Android and have built a UI that
I noticed that all built-in constraints have an empty value for the validatedBy parameter
I have built a simple menu in jQuery http://vanquish.websitewelcome.com/~toberua/ Here is a sample of
I have built UI, its like a search engine for BioProcess/Disease--> Genes. e.g., User
I have built Web sites with Python/Django and desktop applications with Objective-C/Cocoa so programming
I have built a site in wordpress and the home page is really screwed
We have built a web application that accepts SOAP messages, does some processing, calls

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.