Store array of objects in PHP session

804

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!!

369

Answer

Solution:

<?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

76

Answer

Solution:

How has no one seen this:

{-code-1}

Make it{-code-2}

You also need another 2} to close yourif() andfor() 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); 
        }
    }
}
644

Answer

Solution:

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;
}
786

Answer

Solution:

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);

People are also looking for solutions to the problem: Saving 2 textbox array in database PHP/MYSQL

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.