Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9058775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:48:23+00:00 2026-06-16T14:48:23+00:00

I have an SPLObjectStorage object with a Player object as the key and score

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T14:48:24+00:00Added an answer on June 16, 2026 at 2:48 pm

    SplObjectStorage is a key->value store and The “key” of an element in the SplObjectStorage is 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 Using SplStack

    Imagine your player class

    class Player {
        private $name;
        function __construct($name) {
            $this->name = $name;
        }
        function __toString() {
            return $this->name;
        }
    }
    

    Using SplStack

    $group = new SplStack();
    $group->push(new Player("Z"));
    $group->push(new Player("A"));
    $group->push(new Player("B"));
    $group->push(new Player("C"));
    
    echo "<pre>";
    $infinite_it = new InfiniteIterator($group);
    $limit_it = new LimitIterator($infinite_it, 0, 3); // get frist 3
    foreach ( $limit_it as $p ) {
        echo ("$p");
    }
    

    If you insist on SplObjectStorage then you can consider a custom ReverseArrayIterator

    class ReverseArrayIterator extends ArrayIterator {
        public function __construct(Iterator $it) {
            parent::__construct(array_reverse(iterator_to_array($it)));
        }
    }
    

    Usage

    $group = new SplObjectStorage();
    $group->attach(new Player("Z"));
    $group->attach(new Player("A"));
    $group->attach(new Player("B"));
    $group->attach(new Player("C"));
    
    echo "<pre>";
    $infinite_it = new InfiniteIterator(new ReverseArrayIterator($group));
    $limit_it = new LimitIterator($infinite_it, 0, 3); // get frist 3
    foreach ( $limit_it as $p ) {
        echo ("$p");
    }
    

    Both would Output

    CBA //reversed 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SplObjectStorage instance that stores element objects to be rendered in a
So I have this class: class JSObjectStorage extends \SplObjectStorage { /**Adds a JavaScript object
I have a little situation, I am using the SplObjectStorage object and at some
Have the following code: String s= v.request(engine/?key=, P4z72NmBa91&method=load); JSONParser parser = new JSONParser(); Object
have written this little class, which generates a UUID every time an object of
have a nice day. I got problem when trying to create an image from
have numerous tests working, but my actual test of some objects is failing and
Have a photography site that I want to prevent image copying from. How can
Have a script to add / remove options from a select field -- the
Have a linking (or ref) table which has a dual primary key. Need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.