title: Limit array to 5 items
body: <p>I have a code that will add a number to an array each time a page is visited. the numbers are stored in a cookie and are retrieved later.</p>
  
  <p>I would like to keep only the 5 most recent numbers in the array.</p>
  
  <p>if the array is full (5 items) and a new number must be added, then the oldest number must be removed and the most recent items must be kept</p>
  
  <p>here's what i have:</p>
  
  <pre><code>    $lastviewedarticles = array();
  
  if (isset($_COOKIE["viewed_articles"]) ) {
    $lastviewedarticles = unserialize($_COOKIE["viewed_articles"]);
  }
  
  if (!in_array($articleid, $lastviewedarticles)){
      $lastviewedarticles[] = $articleid;
  }
  setcookie("viewed_articles", serialize($lastviewedarticles));
  </code></pre>