开发者

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

开发者 https://www.devze.com 2023-04-08 00:58 出处:网络
I\'m trying to achieve the following I have post data which isan array[] I\'m using a foreach to run through the array

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

精彩评论

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