如何在PHP中使用数组中的多个值的循环与名称前的数组框?
我现在是编程和学习PHP和Yii2的新手。我需要为数组中的多个值使用循环。我在这里使用名称前面的数组框。如何在PHP中使用数组中的多个值的循环与名称前的数组框?
我输入的字段包括: -
<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']
。我怎样才能做到这一点 ?
您应该使用$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']);
}
我使用了一个foreach循环..我怎样才能保存这个值使用foreach循环在这里?你能帮助我吗?在使用for循环如何计算属性在这里之前for循环? –
你的代码是基于for循环..你指的是哪个代码? – scaisEdge
ok !!在for循环中我怎样才能算出数值呢?它在计数值时显示错误 –
抱歉,但你是什么意思与“阵列盒”? – rebecca
[]
所以你想索引?例如,从这里得到0:'HrmRemuneration [0] ['salaryHead']' – rebecca