如何在PHP的$ _POST参数中使用变量 [英] How to use variable inside the parameter of $_POST in php
问题描述
我将获得一定数量的数据.我将获得要为其运行for循环的数字.
I will be getting some certain amount of data. I will get the number for which the for loop to be run.
例如
我得到数字3,我将得到三个参数,例如par1,par2和par3,那么我应该如何在$_post
For example
I get the number 3 and I will get three parameters like par1,par2 and par3 then how should I pass this par1
in the $_post
喜欢
$i=$_POST["number"];
for($i=1;$i<=$n;$i++)
{
$par.$i = $_POST["par".$i];
}
在这里我无法从$_POST["par".$i]
获取值;
由于无法在$_POST
Here I cant get the value from $_POST["par".$i]
;
As it is not able to get the variable inside the paramater of $_POST
任何帮助都会感激
推荐答案
我建议您创建一个新数组$par
,并在其中按索引放置所有具有以下内容的参数:
I suggest that you create a new array $par
and there you will put by index all the par you will have like this:
$i=$_POST["number"];
$par = [];
for($i=1;$i<=$n;$i++)
{
$par[$i] = $_POST["par".$i];
}
在那之后,如果你想扔掉所有的par,你可以像这样使用foreach
:
After that if you want to go throw all pars you can simply use foreach
like this:
foreach($par as $key => $value) {
// $key will be 1,2,3
// $value will be the value from $_POST["par" . $i]
}
.
用于连接PHP中的两个字符串,并且您不能像尝试那样创建新变量.如果要包含在$par1
,$par2
和$par3
中,可以这样:
The .
is to concatenate two strings in PHP, and you can't create a new variable like you tried. If you want to have in $par1
, $par2
and $par3
you can do like this:
${"par" . $i} = $_POST["par".$i];
但是我不推荐这种方式,因为它更难处理.
But I don't recommend this way because it's more hard to handle.
这篇关于如何在PHP的$ _POST参数中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!