Jump to content

Manipulating images array - strange results


gebeer
 Share

Recommended Posts

Hi All,

I have an images field for multiple images set to always return an array.

My images field holds 5 images.

I'm trying to get the first image of the array and then remove that to get an array that holds images 2-5.

Here's my testing code so far

                $images = $page->images;
                $firstimage = $images->eq(0); //also tried $images->first() with same result
                $restimages = $images->remove($firstimage);
                echo count($images) . " images<br>";
                foreach ($images as $img) {
                    echo $img->basename . "<br>";
                }
                echo "firstimage<br>";
                echo $firstimage->basename . "<br>";
                
                echo "restimages<br>";
                foreach ($restimages as $img) {
                    echo $img->basename . "<br>";
                }

And here's the output:

4 images
mario_florenz_1.jpg
mario_florenz_3.jpg
mario_florenz_2.jpg
mario_florenz_5-1.jpg
firstimage
mario_florenz_4.jpg
restimages
mario_florenz_1.jpg
mario_florenz_3.jpg
mario_florenz_2.jpg
mario_florenz_5-1.jpg 

A few things that buffle me:

-count($images) returns 4 where it should be 5

-foreach through $images returns only 4 images where it should be 5

-$images->eq(0) returns the first image but this one is not returned when I loop through all images

-$images and $images->remove($firstimage) return the same

I really don't understand what is happening here. Reading through the API docs on images and WireArray manipulation it seems my code should work.

Any help would be much appreciated.

Link to comment
Share on other sites

Yeah, stupid me :-[

Forgot that $images will be altered after removing.

When I do

                $images = $page->images;
                echo count($images) . " images<br>";
                foreach ($images as $img) {
                    echo $img->basename . "<br>";
                }
                $firstimage = $images->eq(0);
                echo "firstimage<br>";
                echo $firstimage->basename . "<br>";
                
                $restimages = $images->remove($firstimage);
                echo "restimages<br>";
                foreach ($restimages as $img) {
                    echo $img->basename . "<br>";
                }

everything works as expected

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...