如何在php中从数组数组的select选项中获取值?

时间:2022-05-01 09:02:52

I have a select option form field with multiple="true" attribute inside a while loop. If the select option should be multiple chosen we should put a square bracket like [] to the name.

我有一个选择选项表单字段,在while循环中有多个=“true”属性。如果select选项应该是多个选择,我们应该在名称上放置一个方括号,如[]。

Ref: How to get multiple selected values of select box in php?

参考:如何在php中获取选择框的多个选定值?

while($rowdocmap=$resdocmap->fetch_object()){
<select class="form-control" name="selMonths[]" multiple="multiple" required="required"> 
<?php $lsmonths=explode(",", $rowdocmap->DM_Months);
for ($m=1; $m<=12; $m++) {
$month = date('F', mktime(0,0,0,$m, 1, date('Y')));
?>
<option value="<?php echo sprintf("%02d", $m); ?>" <?php if(in_array($m, $lsmonths)) echo 'selected="selected"'; ?>><?php echo $month?></option> <?php } ?> </select>
}

In this type of scenario how can i get values from each select box array

在这种类型的场景中,我如何从每个选择框数组中获取值

1 个解决方案

#1


1  

Your form field name is 'selMonths' so (without multiple) will exist in PHP when your form is sumbitted as:

您的表单字段名称为'selMonths',因此当您的表单被汇总为时,PHP中将存在(不含多个):

$_POST['selMonths'];

This will be the same with the multiple option, although this time it will be an array, and you will have to loop through it to process each selected opton:

这与multi选项相同,虽然这次它将是一个数组,你将不得不循环遍历它来处理每个选定的opton:

foreach($_POST['selMonths'] as $selectedMonth){

    // Do something with $selectedMonth

}

With multiple select boxes in a loop, you are going to need a way to identify each select box. Use an element from your loop ($rowdocmap->id ? ) and add this to the naming convention of your select boxes in the first set of square brackets, to create a multi-dimensional array when submitted.

通过循环中的多个选择框,您将需要一种方法来标识每个选择框。使用循环中的元素($ rowdocmap-> id?)并将其添加到第一组方括号中的选择框的命名约定,以在提交时创建多维数组。

<select name="selMonths[$rowdocmap->id][]" multiple="multiple" required="required">
...

Then you will be able to access them in a simillar way

然后,您将能够以类似的方式访问它们

foreach($_POST['selMonths'] as $selectBox){
    foreach($selectBox as $key => $selectBoxOption){

        // Do something with $key ($rowdocmap->id)
        // and $selectedMonth (selected option)

    }
}

#1


1  

Your form field name is 'selMonths' so (without multiple) will exist in PHP when your form is sumbitted as:

您的表单字段名称为'selMonths',因此当您的表单被汇总为时,PHP中将存在(不含多个):

$_POST['selMonths'];

This will be the same with the multiple option, although this time it will be an array, and you will have to loop through it to process each selected opton:

这与multi选项相同,虽然这次它将是一个数组,你将不得不循环遍历它来处理每个选定的opton:

foreach($_POST['selMonths'] as $selectedMonth){

    // Do something with $selectedMonth

}

With multiple select boxes in a loop, you are going to need a way to identify each select box. Use an element from your loop ($rowdocmap->id ? ) and add this to the naming convention of your select boxes in the first set of square brackets, to create a multi-dimensional array when submitted.

通过循环中的多个选择框,您将需要一种方法来标识每个选择框。使用循环中的元素($ rowdocmap-> id?)并将其添加到第一组方括号中的选择框的命名约定,以在提交时创建多维数组。

<select name="selMonths[$rowdocmap->id][]" multiple="multiple" required="required">
...

Then you will be able to access them in a simillar way

然后,您将能够以类似的方式访问它们

foreach($_POST['selMonths'] as $selectBox){
    foreach($selectBox as $key => $selectBoxOption){

        // Do something with $key ($rowdocmap->id)
        // and $selectedMonth (selected option)

    }
}