Solved

Keeping only the last 3 items in Session array Laravel

Posted on 2016-09-16
3
113 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 54

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 54

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

777 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