开发者

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

I'm trying to achieve the following

I have post data which is an array[]

I'm using a foreach to run through the array

foreach($images as $image){

  update_option( 'image-'. $mycounter .'', $image );

}

I need $mycounter to be counting as it runs through the foreach. How can I achieve that. I tried it with a while loop but not working :(

Tha开发者_JS百科nks


You could use the incrementing operator:

$mycounter = 0;

foreach($images as $image){

  update_option( 'image-'. $mycounter .'', $image );
  $mycounter++; // increments through each iteration

}

OR use the unique numerical indices as your counter, assuming the indices are numerical.

foreach($images as $index => $image){

  update_option( 'image-'. $index .'', $image );

}


$mycounter = 0;
foreach($images as $image){

  update_option( 'image-'. $mycounter .'', $image );
  $mycounter++;
}

Like that?


Have you tried incrementing $mycounter every iteration?

$mycounter = 0;

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

Or am I misunderstanding the question?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜