Jump to content

Getting index from page images array in for loop doesn't work


Tyssen
 Share

Recommended Posts

I'm trying to do this:

        $img_w_0 = 312;
        $img_w_1 = 485;
        $img_w_2 = 380;

        foreach ($page->images as $i=>$image):

            $contentImages .= 'Width = '. ${'img_w_'.$i};

which produces Width = Width = Width = and no value.

If I instead do this:

        $images = array(
    		'0.jpg',
		    '1.jpg',
		    '2.jpg'
		);

        foreach ($images as $i=>$image):

then I get the correct values printed out, i.e. Width = 312, Width = 485, Width = 380.

Why does the index not work for the page images array?

Link to comment
Share on other sites

6 hours ago, Tyssen said:

Why does the index not work for the page images array?

That's because it's not a regular array but a PageImages object. If you inspect $i in your first example, you'll see that it contains the image filename, not a numeric index. To get a plain array without the filenames as keys, use its inherited getValues() method.

	foreach ($page->images->getValues() as $i => $image):

 

  • Like 5
  • Thanks 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...