Increment Value $k 使用 Array Insert 循环
Increment Value $k use Array Insert loop
每次插入该循环的记录时,我都需要增加 $k 变量的值,以便插入具有必要值的数组。这里我留下一部分代码,看看大家能不能帮到我。
$k = 0;
foreach ($detalles as $d) {
$num = $d['stock'];
$val = floor($num/$limite);
for($i=0;$i<$limite;$i++) {
$arr[$i] = $val;
}
$arr[0] += $num - array_sum($arr);
$this->transac_detail_temp->save([
'id_trx_header'=> $id,
'producto' => $d['id_producto'],
'cantidad' => $arr[$k]++,
'tipo_transaccion'=> 2]
);
} // foreach detalles
我不确定你的问题,但希望是你
替换此代码
'cantidad' => $arr[$k]++,
到
'cantidad' => $arr[$k],
和上一节中的这段代码
$k++;
所以最终代码会像这样
$k = 0;
foreach ($detalles as $d) {
$k++; //added
//echo $k."<br>"; // if you want to check update value of $k
$num = $d['stock'];
$val = floor($num/$limite);
for($i=0;$i<$limite;$i++) {
$arr[$i] = $val;
}
$arr[0] += $num - array_sum($arr);
$this->transac_detail_temp->save([
'id_trx_header'=> $id,
'producto' => $d['id_producto'],
'cantidad' => $arr[$k], //update
'tipo_transaccion'=> 2]
);
}
每次插入该循环的记录时,我都需要增加 $k 变量的值,以便插入具有必要值的数组。这里我留下一部分代码,看看大家能不能帮到我。
$k = 0;
foreach ($detalles as $d) {
$num = $d['stock'];
$val = floor($num/$limite);
for($i=0;$i<$limite;$i++) {
$arr[$i] = $val;
}
$arr[0] += $num - array_sum($arr);
$this->transac_detail_temp->save([
'id_trx_header'=> $id,
'producto' => $d['id_producto'],
'cantidad' => $arr[$k]++,
'tipo_transaccion'=> 2]
);
} // foreach detalles
我不确定你的问题,但希望是你 替换此代码
'cantidad' => $arr[$k]++,
到
'cantidad' => $arr[$k],
和上一节中的这段代码
$k++;
所以最终代码会像这样
$k = 0;
foreach ($detalles as $d) {
$k++; //added
//echo $k."<br>"; // if you want to check update value of $k
$num = $d['stock'];
$val = floor($num/$limite);
for($i=0;$i<$limite;$i++) {
$arr[$i] = $val;
}
$arr[0] += $num - array_sum($arr);
$this->transac_detail_temp->save([
'id_trx_header'=> $id,
'producto' => $d['id_producto'],
'cantidad' => $arr[$k], //update
'tipo_transaccion'=> 2]
);
}