使用foreach循环initalize变量 [英] using a foreach loop to initalize variables

查看:124
本文介绍了使用foreach循环initalize变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个空的关联数组卫生组织键名是指提交后的数据。我可以捕获POSTDATA就好了,但我遇到麻烦试图实例变量谁的名字相匹配的数组键。

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.

例如:

$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;
    }       
}

第一行创建的空数组,然后在foreach通过这个数组循环。现在,它变得非常棘手。

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

filter_input(INPUT_POST,$键)捕捉,rUsername在这种情况下,位于后数据匹配当前键的值

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

$键是问题所在。我希望新的变量的名称是联想主要的名称,比如我想替换第一次迭代用$ rUsername $键,$ rPass在第二个,依此类推。我试着用两个$$,但我知道那是不对的。从来没有试过之前这样做,但是这将是有益的,如果我能找到答案。

$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:

这是最后的code这是两个提供答案的组合。

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!

推荐答案

你没有访问$ _ POST可言,所以你正在做的是考虑你自己定义的一些数组成员,他们过滤有害POST字符(为什么你会试图注入自己的code?),然后创建从这些自定义的键值新的数组。

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]));
}

该用stripslashes的建议你在PHP的新空房禁地版本,其中有magic_quotes的支持。你应该升级到最新版本的PHP和/或将其关闭。

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.

这篇关于使用foreach循环initalize变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆