cwd
cwd

Reputation: 54786

PHP - how to keep the keep last ten of something in an array?

I'm using PHP and I'm trying to keep an array of the last 10 post id's in Wordpress via $_SESSION array. I know I can add the latest post id like so:

$_SESSION['recently_viewed_posts'][] = $post->ID;

And similarly I could probably follow that command with something like this to remove ones greater than 10:

if( sizeof( $_SESSION['recently_viewed_posts'] ) > 10 )
{
  array_shift( $_SESSION['recently_viewed_posts'] );
}

However this won't work well if the user reloads the same post a few times, you could end up with something like:

Array
(
    [recently_viewed_posts] => Array
        (
            [0] => 456
            [1] => 456
        )

)

Desired behavior:

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.

What is the best way to accomplish this?

I tried searching for similar questions but didn't come up with anything useful so I apologize if this is a dupe.

Upvotes: 0

Views: 159

Answers (4)

Luke Stevenson
Luke Stevenson

Reputation: 10341

# Get the Existing Post History, if there is one, or an empty array
$postHistory = ( isset( $_SESSION['recently_viewed_posts'] ) ? $_SESSION['recently_viewed_posts'] : array() );

# Remove prior visits
if( $oldKey = array_search( $post->ID , $postHistory ) )
  unset( $postHistory[$oldKey] );

# Add the Post ID to the end of it
$postHistory[] = $post->ID;

# Trim the array down to the latest 10 entries
$postHistory = array_values( array_slice( $postHistory , -10 ) );

# Return the value into the Session Variable
$_SESSION['recently_viewed_posts'] = $postHistory;

Upvotes: 0

xkeshav
xkeshav

Reputation: 54022

TRY something like this ( not tested, just logic)

function addNew($new,$array)
{
  if(!in_array($new,$array))
  {
    if(sizeof($array) < 10){
     array_push($array,$new);
   }
   else{
    array_shift($array);
    addNew($new,$array);
  }
}

addNew($post->ID,$_SESSION['recently_viewed_posts'])

Upvotes: 0

deceze
deceze

Reputation: 522155

if (!isset($_SESSION['recently_viewed_posts'])) {
    $_SESSION['recently_viewed_posts'] = array();
}
array_unshift($_SESSION['recently_viewed_posts'], $post->ID);
$_SESSION['recently_viewed_posts'] =
    array_slice(array_unique($_SESSION['recently_viewed_posts']), 0, 10);

This pushes new entries onto the beginning of the array, weeds out duplicates using array_unique (which keeps the first occurrence of an item) and limits the array to 10 entries. The most recent post will be at $_SESSION['recently_viewed_posts'][0].

Upvotes: 1

xdazz
xdazz

Reputation: 160853

Use $post->ID as the key will make things more simple.

if (sizeof( $_SESSION['recently_viewed_posts'] ) >= 10) {
    array_shift($_SESSION['recently_viewed_posts']);
}

if (isset($_SESSION['recently_viewed_posts'][$post->ID])) {
    unset($_SESSION['recently_viewed_posts'][$post->ID]);
}

$_SESSION['recently_viewed_posts'][$post->ID] = 1;

Then array_keys($_SESSION['recently_viewed_posts']) will give you the result.

Upvotes: 1

Related Questions