我正在尝试获取与数组同名的多个输入字段的值。但场景是其中一个字段是多选的,它本身有多个值。我希望这些可以在不同的数组中获得多个选定的值。我想说的是,如果我在多选1中选择2个值,在多选2中选择3个值。
我应该得到像这样的值
array(
[0] => array(
[0] => value 1
[1] => value 2
[1] => array(
[0] => value 3
[1] => value 4
[5] => value 5
)
)
)
但现在我得到了像这样的值
array(
[0] => value 1
[1] => value 2
[2] => value 3
[3] => value 4
[4] => value 5
)
这是我的代码
View.php
<label for="dependent_benefit"><?php echo "Benefit Name" ?></label>
<select name="dependent_benefit[]" id="dependent_benefit" class="form-control" multiple="multiple">
<?php
foreach ($benefitlist as $list) { ?>
<option value="<?php echo $list->benefit_id ?>"><?php echo $list->benefit_name ?></option>
<?php }
?>
</select>
<label for="dependent_benefit"><?php echo "Benefit Name" ?></label>
<select name="dependent_benefit[]" id="dependent_benefit" class="form-control" multiple="multiple">
<?php
foreach ($benefitlist as $list) { ?>
<option value="<?php echo $list->benefit_id ?>"><?php echo $list->benefit_name ?></option>
<?php }
?>
</select>
表单提交后Controller.php
$dependent_benefit = $this->input->post('dependent_benefit', true);
在表单输入名称中添加一个数字,表示数组的位置。
<select name="dependent_benefit[0][]" id="dependent_benefit" class="form-control" multiple="multiple">
<select name="dependent_benefit[1][]" id="dependent_benefit" class="form-control" multiple="multiple">
只需使用带有偏移量的2d数组,如:
<select name="dependent_benefit[0][]" id="dependent_benefit" class="form-control" multiple="multiple">
<select name="dependent_benefit[1][]" id="dependent_benefit" class="form-control" multiple="multiple">