0

This one is really bugging me, and can not find a easy solution.

On a detail view of a product, i set the info to a session, maximum 4:

$_SESSION['recent'][] = array(
      'id' => $productimgfolder,
      'title' => $product['Product']['title'],
      'link' => $_SERVER['REQUEST_URI'],
      'image' => 'img/products/'.$productimgfolder.'/'.$product['Product']['mainpicture']
    );
$_SESSION['recent'] = array_slice($_SESSION['recent'],-4);

This part works, if i output the session: edit image => this is wat happens if i reload the detail view

enter image description here

The part i'am struggling with is, when i reload a detail view, the info in the session is duplicated.

How can i prevent this from happening?

I tried it with in_array & array_unique, i'am doing something wrong

4
  • Can't you specify a key for the array you're adding to $_SESSION['recent']? It is going to help you prevent duplicates Commented Apr 27, 2015 at 7:01
  • Please share the duplicated data or the keys that you find on reload Commented Apr 27, 2015 at 7:03
  • Please provide what you hope your session to look like after a refresh. Commented Apr 27, 2015 at 7:03
  • What if you have "A B C D" and then load "C" ... should it show "B C D C" or "A B C D"? Commented Apr 27, 2015 at 7:18

3 Answers 3

5

The easy solution:

if the id is unique, you can do like this:

if(!array_key_exists ($productimgfolder, $_SESSION['recent']))
{
    $_SESSION['recent'][$productimgfolder] = array(
          'id' => $productimgfolder,
          'title' => $product['Product']['title'],
          'link' => $_SERVER['REQUEST_URI'],
          'image' => 'img/products/'.$productimgfolder.'/'.$product['Product']['mainpicture']
        );
}

$_SESSION['recent']=array_slice($arr, -4, 4, true);

other wise you have to foreach the recent array and check for id in the loop...

Sign up to request clarification or add additional context in comments.

5 Comments

You can't use custom key, because array_slice will overwrite them. Try it.
Why did someone rate down this answer? It's the proper way. @Kristiyan, last param of array_slice is the flag to preserve key
@Kristiyan I have updated the answer, this is how you can use array_slice
thx Ali, this solution works, its preventing duplacte entries.i learned from you.
Yes, my mistake. I just tried your code and it's doesn't work. Now it's ok.
0

A different solution may be;

array_unshift($sessionArray, $singleArrayElement);
if (count($sessionArray) > 4) {
    array_pop($seassionArray);
};

Comments

0

You need to check isset

if(!isset($_SESSION['recent']))
{
        $_SESSION['recent'] = array()
 }

THEN YOU CAN CHECK IF SESSION IS EMPTY

if(empty($_SESSION['recent']))
 {
    //here you add your data
 }

because you are using array push, on page reload it adds data unless you check if session is empty. if yes you add data to your session.

1 Comment

its not about that the session exists, its about to prevent duplicate entries in the session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.