I have an SPLObjectStorage object with a Player object as the key and score as the info associated with it. The player objects get added to the storage in order from highest to lowest score, but I’m now to a point where I need to iterate through them in reverse order.
I also need to be able to loop all the way through starting from a specified offset. I’ve figured this part out below, I just can’t figure out a good way to reverse it first.
// $players_group = new SPLObjectStorage()
// code to add Player and Score would go here
// above lines just for example
# NEED TO REVERSE ORDER PLAYERS GROUP HERE
$infinite_it = new InfiniteIterator($players_group);
$limit_it = new LimitIterator($infinite_it, $current_offset, $players_group->count());
foreach($limit_it as $p){
// properly outputting from offset all the way around
// but I need it in reverse order
}
I would like to avoid having to loop through the storage object and push all of them into an array, then do array_reverse, and then finally proceed with my foreach loop.
SplObjectStorageis akey->value storeand The “key” of an element in theSplObjectStorageis in fact the hash of the object. Sorting and Reverting would require you to extend and write your own implementation but i think you should consider UsingSplStackImagine your player class
Using
SplStackIf you insist on
SplObjectStoragethen you can consider a customReverseArrayIteratorUsage
Both would Output