如何在php中同时从一个表单访问两个数组?

时间:2022-11-28 11:45:10

I receive two arrays from a form. In the .php file, I need to insert the values of each array into a column of the table.
- use the foreach loop to access the elements of one array and complete the insertion for only one column. When I do the same thing for the next array, I find that the corresponding first column elements are null in each row while for the first array, the corresponding second column elements are null. Is there anyway to avoid this and insert the array elements one after the other?
- I understand that foreach cannot be applied to two arrays so is there any other way I can access both the arrays simultaneously for insertion into a table?
Thanks.

我从表单中收到两个数组。在.php文件中,我需要将每个数组的值插入到表的列中。 - 使用foreach循环访问一个数组的元素,并仅完成一列的插入。当我为下一个数组做同样的事情时,我发现每行中相应的第一列元素为空,而对于第一个数组,相应的第二列元素为空。反正有没有避免这种情况并一个接一个地插入数组元素? - 我知道foreach不能应用于两个数组,所以有没有其他方法可以同时访问这两个数组以插入表中?谢谢。

1 个解决方案

#1


0  

You can use a foreach loop:

你可以使用foreach循环:

 foreach($_POST['someField_1'] as $key => $value)
 {
     $fieldOne = $value;
     $fieldTwo = $_POST['someField_2'][$key];
 }

Obviously change the _POST variables to whatever you've named the fields. As long as your field names are named something like: name="someField_1[]" and name="someField_2[]" You can use the foreach loop in this way.

显然,将_POST变量更改为您已命名字段的任何内容。只要你的字段名称被命名为:name =“someField_1 []”和name =“someField_2 []”你可以用这种方式使用foreach循环。

EDIT

Take this HTML for your input form:

将此HTML用于输入表单:

Forename: <input type="text" name="forename[]"> Surname: <input type="text" name="surname[]">
Forename: <input type="text" name="forename[]"> Surname: <input type="text" name="surname[]">
Forename: <input type="text" name="forename[]"> Surname: <input type="text" name="surname[]">

So that form allows you to enter forenames and surnames for up to 3 people. Each field has the exact same name: forename[] and surname[]

因此,该表单允许您输入最多3人的姓氏和姓氏。每个字段都具有完全相同的名称:forename []和surname []

To retrieve each of the values, you would use this PHP:

要检索每个值,您将使用此PHP:

foreach($_POST['forename'] as $key => $forename)
{
    echo 'Forename: ' . $forename;
    echo ' ';
    echo 'Surname: ' . $_POST['surname'][$key] . '<br />';
}

This works because when you submit a field like forename[], with square brackets at the end, PHP will automatically convert this to an array. $key is a number which starts at 0, and goes on for however many fields there are.

这是有效的,因为当您提交像forename []这样的字段时,最后用方括号,PHP会自动将其转换为数组。 $ key是一个从0开始的数字,然后继续有很多字段。

PHP uses the $key to retrieve the correct fields. You could also write your HTML like this:

PHP使用$ key来检索正确的字段。您也可以像这样编写HTML:

Forename: <input type="text" name="forename[0]"> Surname: <input type="text" name="surname[0]">
Forename: <input type="text" name="forename[1]"> Surname: <input type="text" name="surname[1]">
Forename: <input type="text" name="forename[2]"> Surname: <input type="text" name="surname[2]">

You see I've put a number between the square brackets? PHP will detect this and loop through the arrays.

你看我在方括号之间放了一个数字? PHP将检测到这一点并循环遍历数组。

You could even use a for loop:

你甚至可以使用for循环:

for($i = 0; $i<count($_POST['forename']); $i++)
{
    echo 'Forename: ' . $_POST['forename'][$i];
    echo 'Surname: ' . $_POST['surname'][$i];
}

#1


0  

You can use a foreach loop:

你可以使用foreach循环:

 foreach($_POST['someField_1'] as $key => $value)
 {
     $fieldOne = $value;
     $fieldTwo = $_POST['someField_2'][$key];
 }

Obviously change the _POST variables to whatever you've named the fields. As long as your field names are named something like: name="someField_1[]" and name="someField_2[]" You can use the foreach loop in this way.

显然,将_POST变量更改为您已命名字段的任何内容。只要你的字段名称被命名为:name =“someField_1 []”和name =“someField_2 []”你可以用这种方式使用foreach循环。

EDIT

Take this HTML for your input form:

将此HTML用于输入表单:

Forename: <input type="text" name="forename[]"> Surname: <input type="text" name="surname[]">
Forename: <input type="text" name="forename[]"> Surname: <input type="text" name="surname[]">
Forename: <input type="text" name="forename[]"> Surname: <input type="text" name="surname[]">

So that form allows you to enter forenames and surnames for up to 3 people. Each field has the exact same name: forename[] and surname[]

因此,该表单允许您输入最多3人的姓氏和姓氏。每个字段都具有完全相同的名称:forename []和surname []

To retrieve each of the values, you would use this PHP:

要检索每个值,您将使用此PHP:

foreach($_POST['forename'] as $key => $forename)
{
    echo 'Forename: ' . $forename;
    echo ' ';
    echo 'Surname: ' . $_POST['surname'][$key] . '<br />';
}

This works because when you submit a field like forename[], with square brackets at the end, PHP will automatically convert this to an array. $key is a number which starts at 0, and goes on for however many fields there are.

这是有效的,因为当您提交像forename []这样的字段时,最后用方括号,PHP会自动将其转换为数组。 $ key是一个从0开始的数字,然后继续有很多字段。

PHP uses the $key to retrieve the correct fields. You could also write your HTML like this:

PHP使用$ key来检索正确的字段。您也可以像这样编写HTML:

Forename: <input type="text" name="forename[0]"> Surname: <input type="text" name="surname[0]">
Forename: <input type="text" name="forename[1]"> Surname: <input type="text" name="surname[1]">
Forename: <input type="text" name="forename[2]"> Surname: <input type="text" name="surname[2]">

You see I've put a number between the square brackets? PHP will detect this and loop through the arrays.

你看我在方括号之间放了一个数字? PHP将检测到这一点并循环遍历数组。

You could even use a for loop:

你甚至可以使用for循环:

for($i = 0; $i<count($_POST['forename']); $i++)
{
    echo 'Forename: ' . $_POST['forename'][$i];
    echo 'Surname: ' . $_POST['surname'][$i];
}