问题描述
我已经建立了一个空的关联数组,其关键字引用了提交的发布数据。我可以很好地捕获postdata,但是遇到麻烦,试图实例化名称与数组键匹配的变量。
例如:
$ insArray =阵列( 'rUsername'=> '', 'rPass'=> '', 'rQuestion'=> '',rAnswer '=>' ' 'rFName'=> '', 'rLName'=> '', 'rBDateD'=> '', 'rBDateM'=> '', 'rBDateY'=>' ','rHCheck'=>'','rHCeckOption'=>'','rEmail'=>'');
$ b $ foreach($ insArray as $ key => $ value){
if(filter_input(INPUT_POST,$ key)!=''){
$ key = stripslashes filter_input(INPUT_POST,$ key));
$ insArray [$ key] = $ key;
$ b $ p
$ b
第一行创建空数组,然后foreach循环通过这个数组。现在变得棘手。
捕获位于数据匹配当前键,在这种情况下,用户名是
$ b $ p $ filter $ input $($ INPUT_POST,$ key)
$ key
是问题所在。我希望新变量的名称是关联键名,例如我想在第一次迭代中用$ rUsername替换$ key,在第二次中用$ rPass替换,等等。我尝试使用两个$$,但我知道这是不对的。从来没有尝试过这样做,但是如果我能弄明白,这将是有帮助的。 / p>
这是最后一个代码,它是两个答案的组合。
$($ set $($ _ POST ['submit'])){
//建立要放入数据库的变量数组$ b $ insArray = array('rUsername'=>'' , 'rPassword'=> '', 'rQuestion'=> '', 'rAnswer'=> '', 'rFName'=> '', 'rLName'=> '', 'rBDateD'= > '', 'rBDateM'=> '', 'rBDateY'=> '', 'rHCheck'=> '', 'rHCheckOption'=> '', 'rEmail'=> '') ;
$ b $ foreach(array_keys($ insArray)as $ key){
$ insArray [$ key] = filter_input(INPUT_POST,$ key);
$$ key = filter_input(INPUT_POST,$ key);
给了我想要的输出,谢谢你们!
解决方案你根本没有访问$ _POST,所以你只需要自己定义一些数组成员,过滤他们的有害POST字符(为什么你会尝试注入自己的代码?),然后从这些自定义键值创建一个新的数组。
如果我'这个应该是这样的:
$ b $ $ pre $ foreach(array_keys($ insArray)as $ key){
$ insArray [$ key] = stripslashes(filter_input(INPUT_POST,$ _POST [$ key]));
$ b $ p
$ b使用stripslashes表明你正在使用PHP的braindead版本有magic_quotes启用。您应该升级到现代版本的PHP和/或关闭它们。
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!
解决方案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.
这篇关于使用foreach循环来初始化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!