PHP:while或foreach.我想保存一个也使用计数器的输入数据数组


PHP: while or foreach. I want to save an array of input data which also uses a counter

我正在努力实现以下

我有一个数组[]的post数据

我正在使用foreach运行阵列

foreach($images as $image){
  update_option( 'image-'. $mycounter .'', $image );
}

我需要$mycounter在它穿过前臂时进行计数。我怎样才能做到这一点。我尝试了一段时间的循环,但不起作用:(

感谢

您可以使用递增运算符:

$mycounter = 0;
foreach($images as $image){
  update_option( 'image-'. $mycounter .'', $image );
  $mycounter++; // increments through each iteration
}

使用唯一的数字索引作为计数器,假设索引是数字索引。

foreach($images as $index => $image){
  update_option( 'image-'. $index .'', $image );
}
$mycounter = 0;
foreach($images as $image){
  update_option( 'image-'. $mycounter .'', $image );
  $mycounter++;
}

像那样?

您是否尝试过在每次迭代中递增$mycounter

$mycounter = 0;
foreach($images as $image)
{
    update_option('image-' . $mycounter, $image);
    $mycounter++;
}

还是我误解了这个问题?