mindplay.dk Posted November 9, 2012 Share Posted November 9, 2012 I find myself doing a lot of this: <?php $index=0; foreach ($pages as $page): $index++; ?> .... <?php if ($index != $last_index): ?> <hr /> <?php endif; ?> <?php endforeach; ?> I find that I need $index variables often, because foreach($pages as $index=>$page) does not work - because $index is the Page ID and not the item index. Why is that? I can get to the Page ID in any number of other ways, if I need it - $page->id works fine for most uses. Rarely do I find myself actually needing to access a Page in a PageArray by it's ID - and if I did, $pages->get($page_id) would suffice just fine. Array indices are commonly useful, and they really should be numeric indices, and not some other key. My point is that items in an array do have an index - but that index is hidden/inaccessible during iterations with PageArray, which favors a specific use-case. Numeric indices are generally useful in lots of different use-cases, and in all of those, you're now forced to introduce intermediate variables... I guess it may be too late to change this without breaking backwards compatibility, but maybe something to put on the to-do list for a major release... Link to comment Share on other sites More sharing options...
Soma Posted November 9, 2012 Share Posted November 9, 2012 If($pages->last() === $page).. Link to comment Share on other sites More sharing options...
ryan Posted November 9, 2012 Share Posted November 9, 2012 You've got me confused here. PageArrays are numerically indexed, starting from 0. Try this: foreach($pages->find("id>0, limit=20, sort=random") as $index => $item) { echo "$index: $item<br />"; } 1 Link to comment Share on other sites More sharing options...
Soma Posted November 9, 2012 Share Posted November 9, 2012 Also foreach($parr as $key => $p ) works fine ... Key is not pid. Link to comment Share on other sites More sharing options...
mindplay.dk Posted November 9, 2012 Author Share Posted November 9, 2012 Boy, do I feel dumb now I'm not even sure how I managed to conclude that the indices were IDs... The real problem in my case was that $pages->count returns null, while $pages->count() returns the actual number of items. (unrelated, but that seems to throw me off more often than not...) 1 Link to comment Share on other sites More sharing options...
apeisa Posted November 9, 2012 Share Posted November 9, 2012 I have also stumbled on $pageArray->count many times.. it would be nice little tweak to make it same as count(). Like parent and parent() etc. 1 Link to comment Share on other sites More sharing options...
Soma Posted November 12, 2012 Share Posted November 12, 2012 While at it make the ->first() also work as ->first. 1 Link to comment Share on other sites More sharing options...
diogo Posted November 12, 2012 Share Posted November 12, 2012 And all of those that don't have parameters: last, shuffle, pop, reverse, shift, getRandom, getTotal, etc, etc Link to comment Share on other sites More sharing options...
ryan Posted November 12, 2012 Share Posted November 12, 2012 WireArray types may be associative and are designed to support direct access via indexes. So you could have a collision with a WireArray that has a index of "count" (or one of the other suggested direct access properties). While it's certainly feasible to support that for numerically indexed WireArrays, I'm reluctant to introduce conventions that can't stay consistent across WireArrays. 3 Link to comment Share on other sites More sharing options...
elabx Posted December 11, 2015 Share Posted December 11, 2015 I'm sorry to bring this up from so long time ago, but I'm pulling my hair with this. I'm having the problem mindplay had initially, I can't seem to access the $key value of the items in an array of images, it just returns the name of the image. In this case, logos is an image field containing images, which to my understanding is also a WireArray. I tried what Ryan suggested: foreach( $page->logos as $index => $item) {echo "$index: $item<br />";}But I get this: client3.png: client3.png client4.png: client4.png client2.png: client2.png client6.png: client6.png client1.png: client1.png client5.png: client5.png client8.png: client8.png client7.png: client7.png I'm using PHP 5.6.7 in a MAMP installation on OS X 10.11.1 and latest Processwire 2.7 Link to comment Share on other sites More sharing options...
LostKobrakai Posted December 11, 2015 Share Posted December 11, 2015 The pageimages class is actually keyed by filename, so this is the expected behaviour. To remove the keys and get numbered indexes just use $page->logos->getArray() 1 Link to comment Share on other sites More sharing options...
elabx Posted December 11, 2015 Share Posted December 11, 2015 The pageimages class is actually keyed by filename, so this is the expected behaviour. To remove the keys and get numbered indexes just use $page->logos->getArray() I tried your suggestion in this way: $s = $page->logos->getArray(); foreach($s as $index => $item) { echo "$index: $item<br />"; } It still gives the same result Link to comment Share on other sites More sharing options...
Martijn Geerts Posted December 11, 2015 Share Posted December 11, 2015 You can always use the array_values() function to get an indexed array. array_values($page->logos->getArray()) 1 Link to comment Share on other sites More sharing options...
elabx Posted December 12, 2015 Share Posted December 12, 2015 You can always use the array_values() function to get an indexed array. array_values($page->logos->getArray()) Totally forgot about array_values()! That worked!! Though I wonder if I'm doing something wrong or my dev environment has something funky going on. Thank you both guys! Link to comment Share on other sites More sharing options...
LostKobrakai Posted December 12, 2015 Share Posted December 12, 2015 No funk on your side: getValues() was the method to get the array without the keys. 1 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