Stefanowitsch Posted September 2, 2016 Share Posted September 2, 2016 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 More sharing options...
flydev Posted September 2, 2016 Share Posted September 2, 2016 (edited) 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 September 2, 2016 by flydev object Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 2, 2016 Share Posted September 2, 2016 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 } 2 1 Link to comment Share on other sites More sharing options...
flydev Posted September 2, 2016 Share Posted September 2, 2016 Thanks @LostKobrakai , learning everyday! Link to comment Share on other sites More sharing options...
Stefanowitsch Posted September 2, 2016 Author Share Posted September 2, 2016 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 More sharing options...
LostKobrakai Posted September 2, 2016 Share Posted September 2, 2016 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. 3 Link to comment Share on other sites More sharing options...
Robin S Posted September 3, 2016 Share Posted September 3, 2016 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++; } 3 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now