1

I am working on a plugin. In plugin upload multiple images and save them in an array in database. In previous version when i save them in database i start their indexes with [1][2][3]....

Array
(
    [1] => Array
        (
            [big_img] => site_url/wp-content/uploads/2015/07/Wallpaper_122-big-500x400.jpg
        )

    [2] => Array
        (
            [big_img] => site_url/wp-content/uploads/2015/07/Wallpaper_121-big-500x400.jpg
        )

    [3] => Array
        (
            [big_img] => site_url/wp-content/uploads/2015/07/Wallpaper_123-big-500x400.jpg
        )

)

But in new version i change it and start with [0][1][2]..., it save successfully.

Array
(
    [0] => Array
        (
            [big_img] => site_url/wp-content/uploads/2015/07/Wallpaper_122-big-500x400.jpg
        )

    [1] => Array
        (
            [big_img] => site_url/wp-content/uploads/2015/07/Wallpaper_121-big-500x400.jpg
        )

    [2] => Array
        (
            [big_img] => site_url/wp-content/uploads/2015/07/Wallpaper_123-big-500x400.jpg
        )

)

The issue is when a user upgrade my plugin and go to single page the [0] indexes image miss, it start from [1] index.

Here is my loop code.

<?php
     $count = 0;
     foreach ($thumb_images as $imgs) {
?>
        <li>
           <img src="<?php echo $imgs['thumb_img']; ?>" alt="" data-resize="<?php echo $big_images[$count]['big_img']; ?>" />
        </li>
<?php
     $count++;
     }
?>

Now i just want to change the indexes but not from loop. I don't know how can i change the indexes from [1][2][3]... to [0][1][2]...

So how can i do this.

deemi-D-nadeem
  • 2,183
  • 3
  • 27
  • 67

1 Answers1

2

You can use the following solution:

// big_images the array consisting list of images
foreach($big_images as $k=>$v){
   $big_images[$k-1] = $v;
}
//removing last element from the array
unset($big_images[count($big_images)];
Domain
  • 11,110
  • 2
  • 22
  • 44