1

I am building a shopping cart using session variables. I can push the array to the session array like this:

//initialize session cart array
$_SESSION['cart'] = array();
//store the stuff in an array
$items  = array($item, $qty);
//add the array to the cart
array_push($_SESSION['cart'], $items);

So far, so good. The problem is in removing an item from the cart. When I try to use this, I get a array to string conversion error.

//remove an array from the cart
$_SESSION['cart'] = array_diff($_SESSION['cart'], $items);

To clarify, the question here is why is the above statement creating an array to string conversion error?

16
  • possible duplicate of stackoverflow.com/questions/3600750/… Commented Aug 15, 2014 at 0:00
  • any reason to use array_push over $_SESSION['cart'][]= $items; Commented Aug 15, 2014 at 0:01
  • Not so fast -- not a duplicate at all. Check your info before posting. Commented Aug 15, 2014 at 0:02
  • use the item id as the key and unset that Commented Aug 15, 2014 at 0:03
  • 1
    " Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function. " Commented Aug 15, 2014 at 0:12

2 Answers 2

2

How about storing an array of objects like this. In my opinion it is a lots easier to read the code this way than addressing arrays within arrays

$item = new stdClass();
$item->id = 99;
$item->qty = 1;
$item->descr = 'An Ice Cream';
$item->price = 23.45;

$_SESSION['cart'][$item->id] = $item;

To remove an items from the cart

unset($_SESSION['cart'][$item]);

To re-access the items data

echo $_SESSION['cart'][$item]->id;
echo $_SESSION['cart'][$item]->desc;
echo $_SESSION['cart'][$item]->price;

Or even

$item = $_SESSION['cart'][$item];
echo $item->id;
echo $item->desc;
echo $item->price;

Or even better

foreach ( $_SESSION['cart'] as $id => $obj ) {
    echo $id ' = ' $obj->descr ' and costs ' . $obj->price;
}

To change existing info

$_SESSION['cart'][$item]->qty += 1;

or

$_SESSION['cart'][$item]->qty = $newQty;
Sign up to request clarification or add additional context in comments.

7 Comments

Can you add methods to your object and still store it as a session variable?
No I am afraid not. But if you actually created an class you can save the properties of the object to the session and then reconstitute the object by re-instantiating the class and then rehydrating the properties from a $_SESSION variable. You just do $_SESSION['cart'] = serialize($cartObj); See this documentation
That's what was screwing me up originally then. I started out trying to use an object but had errors because the object had methods. I finally ended up doing this, foreach($_SESSION['cart'] as $k => $v) { if ($v == $itemID) unset($_SESSION['cart'][$k]); } which works, but I'm still wondering why I was getting the conversion error with array_diff();
Do a print_r(array_diff($_SESSION['cart'], $items)); like I suggested earlier. I bet it does not look deep enough into the arrays to do what you hoped.
You might try array_diff($_SESSION['cart'][], $items) that might work.
|
1

i suggest this approach

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

to add an item

$_SESSION['cart'][$item]= $qty;

then use the items id to manipulate:

delete:

unset($_SESSION['cart'][$item]);

change to known qty value:

$_SESSION['cart'][$item]= $qty;

add one:

$_SESSION['cart'][$item] += 1;

multiple variables for an item:

$_SESSION['cart'][$item]= array('qty'=>$qty,$descrip,$size,$colour);

1 Comment

And using this method, how would you add additional variables, i.e. $price, $descr, etc.?

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.