title: PHP - how to keep the keep last ten of something in an array?
body: <p>I'm using PHP and I'm trying to keep an array of the last 10 post id's in Wordpress via <code>$_SESSION</code> array. I know I can add the latest post id like so:</p>
  
  <pre><code>$_SESSION['recently_viewed_posts'][] = $post-&gt;ID;
  </code></pre>
  
  <p>And similarly I could probably follow that command with something like this to remove ones greater than 10:</p>
  
  <pre><code>if( sizeof( $_SESSION['recently_viewed_posts'] ) &gt; 10 )
  {
    array_shift( $_SESSION['recently_viewed_posts'] );
  }
  </code></pre>
  
  <p>However this won't work well if the user reloads the same post a few times, you could end up with something like:</p>
  
  <pre><code>Array
  (
      [recently_viewed_posts] =&gt; Array
          (
              [0] =&gt; 456
              [1] =&gt; 456
          )
  
  )
  </code></pre>
  
  <p>Desired behavior:</p>
  
  <ul>
  <li>The last 10 post id's will be keep in an array</li>
  <li>If a visited post is already in the array, it will move to the start or end of the array</li>
  <li>If the size of the array is 10 elements, and a new 11th post is visited, the oldest post id will be removed and the new post id added</li>
  </ul>
  
  <p>I don't care too much about what side of the array (start or end) the new posts are on, as long as it is consistent. I don't really care what the array keys are.</p>
  
  <p>What is the best way to accomplish this?</p>
  
  <p>I tried searching for similar questions but didn't come up with anything useful so I apologize if this is a dupe.</p>