1

How can I store/remove/get an array of objects in the PHP session?

I tried this for adding:

array_push($_SESSION['cart'], serialize($item));    

and this for removing:

function removeitem ($item)
{
    $arrayCart = $_SESSION['cart'] ;
    for ($i=0; $i<$arrayCart.length; $++ )
    {
        if ($item.id == $arrayCart[i].id)
            $arrayCart.splice (i,0); 
    }
}

But it doesn't work!!

2
  • 1
    Since when did php use dot notation??????? you are confusing js and php. i recommend buying a book, and using an ide Commented Jan 25, 2014 at 10:42
  • the glaring thing I see is use of serialize - this function is for writing to file or db - if your data is native php then keep it as php Commented Jan 25, 2014 at 11:25

4 Answers 4

1
<?php
session_start();

$_SESSION['cart']=array();

$_SESSION['cart']['username'] = 'Admin';
$_SESSION['cart']['Password'] = '123';
.
.
.
?>


For Remove

    <?php
    session_start();

    $_SESSION['cart']=array();

   unset ( $_SESSION['cart']['username'] );
    .
    .

or use Custom Function to Remove...
    .
    ?>

It's Work For Me...
if this code Report Errors Please Comment to Check this...
Please check PHP version

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

Comments

0

How has no one seen this:

for ($i=0; $i<$arrayCart.length; $++ ){ //  <-----  lol @ $++

Make it $i++

You also need another 2 } to close your if() and for() statements so it's like this:

function removeitem ($item){
    $arrayCart = $_SESSION['cart'] ;
    for ($i=0; $i<$arrayCart.length; $++ ){
        if ($item.id == $arrayCart[i].id){
            $arrayCart.splice (i,0); 
        }
    }
}

3 Comments

Yes, you're right. I guess the code will work after your changes, however it isn't very efficient
Agreed but @Tom down below hit it on the head with a more useful answer
Furthermore, if a customer adds one product several times, it is also stored several times in the cart. If the item-id is used as the Array-Index, this can't happen. (and searching for an item also gets more efficient)
0

Have you tried using this?

if(!isset($_SESSION['cart'])){
  $_SESSION['cart'] = [];
}
$_SESSION['cart'][] = $item;

You don't need to serialize the item-variable - a Session can store pure Objects, as long as the max. session-size doesn't exceed.

If you want to remove Items from the cart, you should use index-Names - this allows you to find your items faster.

I would probably use something like this:

if(!isset($_SESSION['cart'])){
  $_SESSION['cart'] = [];
}

function addItem($itemName){
    if(!isset($_SESSION['cart'][$itemName])) {
        $_SESSION['cart'][$itemName] = 1;
    }else
        $_SESSION['cart'][$itemName] ++;
    }
}
function removeItem($itemName){
    if(isset($_SESSION['cart'][$itemName])) {
        if($_SESSION['cart'][$itemName] > 0) {
            $_SESSION['cart'][$itemName] --;
        }
    }
}

function clearCart(){
   unset($_SESSION['cart']);
}

In this case, the cart stores each item and its amount. Instead of $itemName, you could also use the item-ID from your database.

To get an item from the cart, you will need this function:

function getItemCount($itemName){
    if(isset($_SESSION['cart'][$itemName])) {
        return $_SESSION['cart'][$itemName];
    }
    return 0;
}

Comments

0

Try not pushing to array, because its probably not set yet, but to store to session, create new session var by:

$_SESSION['cart']=serialize($item);

to remove it:

unset($_SESSION['cart']);

EDIT:

Thanks to comments I realized that above code wouldn't store array in session. So, another short solution is to store to session var some JSON. Try this for a short test:

$tmp = array(1,2,3);
$_SESSION['cart']=json_encode($tmp);
print_r(json_decode($_SESSION['cart']));

If that array is associative, you shoud use for decode:

json_decode($_SESSION['cart'], true);

2 Comments

but in this way I have only one object in my session..?
This would only allow one item in the cart.

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.