我有一个表单,如下面所示发布到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[]" />
如果我使用下面的代码在PHP中回显出来
$name = $_POST['name'];
$email = $_POST['account'];
foreach( $name as $v ) {
print $v;
}
foreach( $email as $v ) {
print $v;
}
我会得到这样的东西:
name1name2name3email1email2email3
我怎么能得到:
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);
所以我的输出是这样的:
姓名是name1,email是email1,谢谢 。姓名是name2,email是email2,谢谢 。姓名是name3,email是email3,谢谢
感谢您的任何帮助或建议
发布于 2018-01-24 12:52:43
它们已经在数组中:$name
是一个数组,就像$email
所以你所要做的就是添加一些处理来改变这两个数组:
$name = $_POST['name'];
$email = $_POST['account'];
foreach( $name as $key => $n ) {
print "The name is ".$n." and email is ".$email[$key].", thank you\n";
}
要处理更多,只需扩展模式:
$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";
}
发布于 2018-01-24 13:38:24
例如,将字段命名为
<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]" />
(通过javascript添加元素也是可能的)
对应的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);
https://stackoverflow.com/questions/-100007230
复制相似问题