Solved

Keeping only the last 3 items in Session array Laravel

Posted on 2016-09-16
3
48 Views
Last Modified: 2016-09-16
Hi,
I'm using Laravel and keeping track of recently viewed item ID's in a 'recent' array.
To store them I use: Session::push('recent', $id) when a user visits an item details page.

After visiting a few items, my array when dumped looks like:
array:6 [▼
  0 => "17918"
  1 => "18204"
  2 => "17180"
  3 => "18119"
  4 => "17897"
  5 => "18119"
]

I'm only after the last 3 items (just showing last 3 items you visited). How can I store just the last 3 all the time instead of continually adding to the array? I thought I could just 'use' the last 3 items in the array but as a user continues to visit detail pages, the array will just keep getting larger. I was thinking array_slice perhaps to keep it to 3 but couldn't figure out how to implement in this case. Any suggestions/direction would be great.

Thanks as always.
PS: I'm nearly through this project and never would have made it without all the help I've received on this site. Thanks again.
0
Comment
Question by:tjyoung
  • 2
3 Comments
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41801291
array_push the latest item to the array
at the same time array_shift the first item out.
Example
$recent = $request->session()->get('recent');
if (!is_array($recent)) {
   $recent = array();
}
$recent[] = $id;
while (count($recent) > 3)) {
   array_shift($recent);
}
$request->session()->put('recent', $recent);

Open in new window

0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 41801496
Minor changes for Laravel, but worked great. Thanks!
$recent = Session::get('recent');
      if (!is_array($recent)) {
               $recent = array();
            }
      $recent[] = $id;
      while (count($recent) > 3) {
            array_shift($recent);
      }
      Session::put('recent', $recent);
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41801534
You are welcome - I was working off https://laravel.com/docs/5.3/session
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now