我有一个类似下面的表单,该表单发布到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; }
我会得到这样的东西:
名称1名称2名称3电子邮件1电子邮件2电子邮件3
我怎样才能把这些数组变成下面的代码
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 ,电子邮件为 email1 ,谢谢 。名称为 name2 ,电子邮件为 email2 ,谢谢 。名称为 name3 ,电子邮件为 email3 ,谢谢。
感谢您的帮助或建议
它们已经在数组中:$name是数组,是这样$email
$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"; }