Jump to content

Minor annoyance with indices in PageArrays


mindplay.dk
 Share

Recommended Posts

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

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 />";
}
  • Like 1
Link to comment
Share on other sites

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...)

  • Like 1
Link to comment
Share on other sites

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.

  • Like 3
Link to comment
Share on other sites

  • 3 years later...

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

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

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

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...