在PHP中以数组形式发送多个文本字段

在PHP中以数组形式发送多个文本字段,php,arrays,for-loop,input,Php,Arrays,For Loop,Input,我的文件中有以下代码: <table width="100%" class="friends"> <tr> <th width="50%">Friends Name</th> <th>Friends Email</th> </tr> <tr> <td><input type="text" name="friendname[]" id="friendname[]

我的文件中有以下代码:

<table width="100%" class="friends">
<tr>
    <th width="50%">Friends Name</th>
    <th>Friends Email</th>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="jandjdoe@example.com" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="jandjdoe@example.com" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="jandjdoe@example.com" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="jandjdoe@example.com" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="jandjdoe@example.com" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="jandjdoe@example.com" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="jandjdoe@example.com" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="jandjdoe@example.com" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="jandjdoe@example.com" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="jandjdoe@example.com" style="width:80%;" /></td>
</tr>

朋友姓名
朋友电子邮件

下面是我的PHP代码:

if(!empty($_POST['friendname']) && !empty($_POST['friendemail']) && is_array($_POST['friendname']) && is_array($_POST['friendemail']) && count($_POST['friendname']) === count($_POST['friendemail'])) {
    $err[] = "Please enter in your friend's details.";
}

$name_array = $_POST['friendname'];
$email_array = $_POST['friendemail'];
for ($i = 0; $i < count($name_array); $i++) {
    $name = $name_array[$i];
    $email = $name_array[$i];
}
if(!empty($\u POST['friendname'])和&!empty($\u POST['friendemail'])和is_数组($\u POST['friendemail'])和计数($\u POST['friendemail'])==计数($\u POST['friendemail'])){
$err[]=“请输入您朋友的详细信息。”;
}
$name_array=$\u POST['friendname'];
$email\u array=$\u POST['friendemail'];
对于($i=0;$i
然而,我的代码没有响应任何内容

我从你那里得到了上面的代码

我的表单中还有其他字段,它们工作得很好,但由于某些原因,这些字段不工作。有没有人知道根据上面的代码可以实现我想要的功能?:)

foreach(数组\u组合($\u POST['friendname'],$\u POST['friendemail'])为$name=>$email){
echo$name.“-”$email.“
”; }
谢谢@BeatAlex这真的很有帮助!但不幸的是,我的代码显示数组为空。我把echo$_POST['friendname']放在
;echo$_POST['friendemail'];退出()
在我提交代码的顶部,它刚刚返回
ArrayArray
您不能在PHP中“回送”数组。放置变量转储($_POST['friendname']);var_dump($_POST['friendemail']);退出();在你的page@nahime对不起,你是对的。我把它改成了
echo';变量转储($_POST['friendname']);回声';回声';var_dump($_POST['friendemail']);回声';退出()if(!empty($u POST['friendname'])&!empty($u POST['friendemail'])和is_数组($u POST['friendemail'])和计数($u POST['friendemail'])==计数($u POST['friendemail'])){
进行验证,这没有给我任何东西。有什么想法吗?你的
var_dump
返回什么?你正在检查两者的长度是否相同。看看var_dump,看看这是否正确。顺便问一句,你以前有没有尝试过@BeatAlex解决方案而不进行验证?
foreach (array_combine($_POST['friendname'], $_POST['friendemail']) as $name => $email) {
 echo $name . " - " . $email . "<br />";
}