简体   繁体   English

增量值 $k 使用数组插入循环

[英]Increment Value $k use Array Insert loop

I need to increase the value of the $ k variable every time a record of that loop is inserted in order to insert the array I have with the necessary values.每次插入该循环的记录时,我都需要增加 $ k 变量的值,以便插入具有必要值的数组。 Here I leave a part of the code to see if you can help me.这里我留下一部分代码,看看能不能帮到我。

$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

I am not sure about your question but hope this will be you Replace this code我不确定您的问题,但希望这将是您替换此代码

'cantidad' =>  $arr[$k]++,

to

'cantidad' =>  $arr[$k],

and this code in last section和上一节中的这段代码

$k++;

so final code will be like this所以最终的代码会是这样的

$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]
 );

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM