Jump to content

Fieldtype image array: $key returns string, not number


Stefanowitsch
 Share

Recommended Posts

I want to display a gallery and have an array which consists of image fields.

I am iterating through the array which works perfectly fine but I want to have access to the key of the items too.

<? foreach ($data->img_gallery as $key=>$image) { ?>
// some code

But the $key variable in my array returns the filenames (image1.jpg), not the index key (0, 1, 2, ....).


Why is that so? Does it have something to to with the fieldtype (in my case: Image)?
 

When I am using the same functionality for other fieldtypes (Page or Repater) the key delivers the index as supposed.

Link to comment
Share on other sites

If you really want to build an array with index as Integer, you could do something like that :

$array = new WireArray();
foreach ($data->img_gallery as $imageobj) {
    $array->add($imageobj);
}

then :

foreach ($array as $key => $image) {
    echo = $key .' => '. $image->url .'<br>';
}

 

Your object now look like this (exemple with 4 pictures in the image fieldtype) :

ProcessWire\WireArray
data protected => array (4)
    0 => ProcessWire\Pageimage 
    1 => ProcessWire\Pageimage 
    2 => ProcessWire\Pageimage 
    3 => ProcessWire\Pageimage

 

 

1 hour ago, Stefanowitsch said:

Why is that so? Does it have something to to with the fieldtype (in my case: Image)?

I let others answer, it will surely be better.

Edited by flydev
object
Link to comment
Share on other sites

1 hour ago, Stefanowitsch said:

When I am using the same functionality for other fieldtypes (Page or Repater) the key delivers the index as supposed.

There's nowhere written that a WireArray does need to be keyed with integers. For the file fields using the filename as key makes a lot of sense. If you still need the integer value just use this:

foreach ($data->img_gallery->getValues() as $key => $imageobj) {
    // Do stuff
}

 

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

1 hour ago, LostKobrakai said:

There's nowhere written that a WireArray does need to be keyed with integers. For the file fields using the filename as key makes a lot of sense. If you still need the integer value just use this:


foreach ($data->img_gallery->getValues() as $key => $imageobj) {
    // Do stuff
}

 

That works perfectly! I did not know that I have to use the getValues() method to get the indices. Thank you very much.

I am just curious because when I iterate over an array based of the "Page" fieldtype (for example), it is already keyed with integers.

Link to comment
Share on other sites

It depends on the type of WireArray and where it's used. It's just like arrays. One can choose to use string keys, but one can also leave keys be auto determined. $wirearray->getArray() will give you this internal array, $wirearray->getValues() is the internal array run through array_values() and $wirearray->getKeys() will give you the keys used.

  • Like 3
Link to comment
Share on other sites

18 hours ago, Stefanowitsch said:

That works perfectly! I did not know that I have to use the getValues() method to get the indices.

If you want to keep the original Pageimages WireArray but use an index you can create your own index counter:

$index = 0;
foreach($data->img_gallery as $image) {
    // do whatever with $index and $image here
    $index++;
}

 

  • Like 3
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...