如何将表单输入数组转换为 PHP 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3314567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get form input array into PHP array
提问by thom
I have a form like the one below which is posted to contacts.php, and the user can dynamically add more with jquery.
我有一个像下面这样的表单,它被发布到contacts.php,用户可以用jquery动态添加更多。
<input type="text" name="name[]" />
<input type="text" name="email[]" />
<input type="text" name="name[]" />
<input type="text" name="email[]" />
<input type="text" name="name[]" />
<input type="text" name="email[]" />
If i echo them out in php with the code below
如果我用下面的代码在 php 中回显它们
$name = $_POST['name'];
$email = $_POST['account'];
foreach( $name as $v ) {
print $v;
}
foreach( $email as $v ) {
print $v;
}
I will get something like this:
我会得到这样的东西:
name1name2name3email1email2email3
name1name2name3email1email2email3
how can I get those arrays into something like the code below
我怎样才能把这些数组变成类似于下面的代码
function show_Names($n, $m)
{
return("The name is $n and email is $m, thank you");
}
$a = array("name1", "name2", "name3");
$b = array("email1", "email2", "email3");
$c = array_map("show_Names", $a, $b);
print_r($c);
so my output is like this:
所以我的输出是这样的:
The name is name1and email is email1, thank you
The name is name2and email is email2, thank you
The name is name3and email is email3, thank you
名字是name1,email 是email1,谢谢
名字是name2,email 是email2,谢谢
名字是name3,email 是email3,谢谢
thank you for any help or advice
感谢您的任何帮助或建议
回答by Jeffrey Blake
They are already in arrays: $nameis an array, as is $email
它们已经在数组中:$name是一个数组,原样$email
So all you need to do is add a bit of processing to attack both arrays:
所以你需要做的就是添加一些处理来攻击两个阵列:
$name = $_POST['name'];
$email = $_POST['account'];
foreach( $name as $key => $n ) {
print "The name is ".$n." and email is ".$email[$key].", thank you\n";
}
To handle more inputs, just extend the pattern:
要处理更多输入,只需扩展模式:
$name = $_POST['name'];
$email = $_POST['account'];
$location = $_POST['location'];
foreach( $name as $key => $n ) {
print "The name is ".$n.", email is ".$email[$key].
", and location is ".$location[$key].". Thank you\n";
}
回答by VolkerK
E.g. by naming the fields like
例如,通过将字段命名为
<input type="text" name="item[0][name]" />
<input type="text" name="item[0][email]" />
<input type="text" name="item[1][name]" />
<input type="text" name="item[1][email]" />
<input type="text" name="item[2][name]" />
<input type="text" name="item[2][email]" />
(which is also possible when adding elements via javascript)
(这在通过 javascript 添加元素时也是可能的)
The corresponding php script might look like
相应的 php 脚本可能看起来像
function show_Names($e)
{
return "The name is $e[name] and email is $e[email], thank you";
}
$c = array_map("show_Names", $_POST['item']);
print_r($c);
回答by Can O' Spam
I know its a bit late now, but you could do something such as this:
我知道现在有点晚了,但你可以做这样的事情:
function AddToArray ($post_information) {
//Create the return array
$return = array();
//Iterate through the array passed
foreach ($post_information as $key => $value) {
//Append the key and value to the array, e.g.
//$_POST['keys'] = "values" would be in the array as "keys"=>"values"
$return[$key] = $value;
}
//Return the created array
return $return;
}
The test with:
测试与:
if (isset($_POST['submit'])) {
var_dump(AddToArray($_POST));
}
This for me produced:
这对我来说产生了:
array (size=1)
0 =>
array (size=5)
'stake' => string '0' (length=1)
'odds' => string '' (length=0)
'ew' => string 'false' (length=5)
'ew_deduction' => string '' (length=0)
'submit' => string 'Open' (length=4)
回答by Sophia Gavish
What if you've got array of fieldsets?
如果你有一个字段集数组怎么办?
<fieldset>
<input type="text" name="item[1]" />
<input type="text" name="item[2]" />
<input type="hidden" name="fset[]"/>
</fieldset>
<fieldset>
<input type="text" name="item[3]" />
<input type="text" name="item[4]" />
<input type="hidden" name="fset[]"/>
</fieldset>
I added a hidden field to count the number of the fieldsets. The user can add or delete the fields and then save it.
我添加了一个隐藏字段来计算字段集的数量。用户可以添加或删除字段,然后保存。
回答by thatonefreeman
I came across this problem as well. Given 3 inputs: field[], field2[], field3[]
我也遇到了这个问题。给定 3 个输入:field[]、field2[]、field3[]
You can access each of these fields dynamically. Since each field will be an array, the related fields will all share the same array key. For example, given input data:
您可以动态访问这些字段中的每一个。由于每个字段都是一个数组,因此相关字段都将共享相同的数组键。例如,给定输入数据:
- Bob, [email protected], male
- Mark, [email protected], male
- Bob, [email protected], 男
- 马克,[email protected],男
Bob and his email and sex will share the same key. With this in mind, you can access the data in a for loop like this:
Bob 和他的电子邮件和性别将共享相同的密钥。考虑到这一点,您可以像这样在 for 循环中访问数据:
for($x = 0; $x < count($first_name); $x++ )
{
echo $first_name[$x];
echo $email[$x];
echo $sex[$x];
echo "<br/>";
}
This scales as well. All you need to do is add your respective array vars whenever you need new fields to be added.
这也可以缩放。您需要做的就是在需要添加新字段时添加各自的数组变量。
回答by Abhishek Gurjar
Nonetheless, you can use below code as,
尽管如此,您可以使用以下代码作为,
$a = array('name1','name2','name3');
$b = array('email1','email2','email3');
function f($a,$b){
return "The name is $a and email is $b, thank you";
}
$c = array_map('f', $a, $b);
//echoing the result
foreach ($c as $val) {
echo $val.'<br>';
}
回答by Ian
This is easy one:
这是一个简单的:
foreach( $_POST['field'] as $num => $val ) {
print ' '.$num.' -> '.$val.' ';
}
回答by iroel
However, VolkerK's solution is the best to avoid miss couple between email and username. So you have to generate HTML code with PHP like this:
然而,VolkerK 的解决方案是最好的避免电子邮件和用户名之间的遗漏。所以你必须像这样用 PHP 生成 HTML 代码:
<? foreach ($i = 0; $i < $total_data; $i++) : ?>
<input type="text" name="name[<?= $i ?>]" />
<input type="text" name="email[<?= $i ?>]" />
<? endforeach; ?>
Change $total_data to suit your needs. To show it, just like this:
更改 $total_data 以满足您的需要。要显示它,就像这样:
$output = array_map(create_function('$name, $email', 'return "The name is $name and email is $email, thank you.";'), $_POST['name'], $_POST['email']);
echo implode('<br>', $output);
Assuming the data was sent using POST method.
假设数据是使用 POST 方法发送的。
回答by Raphael Gates
Using this method should work:
使用这种方法应该有效:
$name = $_POST['name'];
$email = $_POST['account'];
while($explore=each($email)) {
echo $explore['key'];
echo "-";
echo $explore['value'];
echo "<br/>";
}

