我是编程和学习PHP和Yii2的新手。我需要在数组中使用循环来获取多个值。我在这里使用名字前面的数组框。
我的输入字段是: -
<div class="row thisIsCloned">
<div class="col-lg-5 col-md-5 col-xs-12">
<label for="">Salary Head</label>
<?php
$dept = \yii\helpers\ArrayHelper::map(app\modules\hrm\models\HrmSalaryHead::find()->where(['status'=>1])->all(), 'id', 'salaryHead');
echo Select2::widget([
'name' => 'HrmRemuneration[0][salaryHead]',
'data' => $dept,
'options' => [
'placeholder' => 'Salary Head ...',
'id'=>'salaryHead',
],
'pluginOptions' => [
'allowClear' => true,
],
]);
?>
</div>
<div class="col-lg-4 col-md-4 col-xs-12">
<label for="">Salary Amount in </label> ( <span class="rupee">Rs. </span> <span class="percentage" style="display:none">%</span> )
<input type="checkbox" name="HrmRemuneration[0][checkbox]" value="1" class="status">
<input type="hidden" value="0" name="HrmRemuneration[0][checkbox]" >
<span><input type="number" class="form-control" name="HrmRemuneration[0][salaryAmount]" required=""></span>
</div>
<div class="col-lg-1">
<button id="thisIsClonNewRowButton" type="button" class="pull-right quantity-right-plus btn btn-success" style="margin-top: 28px;height: 27px !important; padding: 3px 6px !important;">
<span class="glyphicon glyphicon-plus"></span>
</button>
</div>
</div>
<div class="renderremuneration"></div>
对于我在POST的控制器: -
public function actionRemuneration()
{
$model=new HrmRemuneration();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$a=($_POST["HrmRemuneration"]);
$count = count($a);
for ($i = 0; $i < $count; $i++) {
$pmodel = new HrmRemuneration();
$pmodel->salaryHead=$model[$i]['salaryHead'];
$pmodel->amountCalc=$model[$i]['amountCalc'];
if(!$pmodel->save())
{
$this->Message('Sorry !! Error occurs in adding Salary Head', 1);
return $this->redirect(['/hrm/settings/addsalaryhead']);
}
}
}
}
else
{
return $this->render('remuneration',['model'=>$model]);
}
}
如何在名称前面发布使用数组框的值?使用输入字段名称HrmRemuneration[0]['salaryHead']
。我怎么能这样做?
答案 0 :(得分:0)
您应该使用$array[$i]['attr_name'];
而不是$array['attr_name'][$i];
$count = count($model);
for ($i = 0; $i < $count; $i++) {
$pmodel = new HrmSalaryHead();
$pmodel->salaryHead=$model[$i]['salaryHead'];
$pmodel->amountCalc=$model[$i]['amountCalc'];
if(!$pmodel->save())
{
$this->Message('Sorry !! Error occurs in adding Salary Head', 1);
return $this->redirect(['/hrm/settings/addsalaryhead']);
}