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?

user1780242
  • 541
  • 1
  • 10
  • 25
  • possible duplicate of http://stackoverflow.com/questions/3600750/is-it-possible-to-delete-an-objects-property-in-php – Dan O Aug 15 '14 at 00:00
  • any reason to use array_push over `$_SESSION['cart'][]= $items;` –  Aug 15 '14 at 00:01
  • Not so fast -- not a duplicate at all. Check your info before posting. – user1780242 Aug 15 '14 at 00:02
  • use the item id as the key and unset that –  Aug 15 '14 at 00:03
  • Wouldn't $_SESSION['cart'][]= $items; simply overwrite the same cell each time it was invoked? – user1780242 Aug 15 '14 at 00:05
  • nope it increments using the next free id. i see now its the first thing mentioned on the array_push page –  Aug 15 '14 at 00:06
  • Is there an advantage to using that method over array_push()? – user1780242 Aug 15 '14 at 00:08
  • 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. " –  Aug 15 '14 at 00:12
  • Do a `print_r(array_diff($_SESSION['cart'], $items));` I dont think that `array_diff` is generating what you hoped it would. – RiggsFolly Aug 15 '14 at 00:17
  • I can see how using the item ID as a key would work with only one value, i.e. qty as in $item=>$qty, but what if I decided to add more to the array, like $item = array($item=>$qty, $qty, $price, $descr)? Would unset then delete the entire array or just $qty? – user1780242 Aug 15 '14 at 00:18
  • `$_SESSION['cart'][$item]= array($qty,$descrip,$size,$colour);` –  Aug 15 '14 at 00:20
  • Aaaah. I like that approach. That seems practical. – user1780242 Aug 15 '14 at 00:21
  • explained in answer below –  Aug 15 '14 at 00:23
  • How about storing an array of objects rather than array of arrays. Its a whole lot easier to understand and read the code when you come back in a few weeks to fix something, – RiggsFolly Aug 15 '14 at 00:23
  • Don't you have to serialize the object to store it in a session variable? – user1780242 Aug 15 '14 at 00:25
  • No serialize necessary. $_SESSION is just like any other array and is automatcially serialized by PHP when it saves $_SESSION to a file. – RiggsFolly Aug 15 '14 at 00:35

2 Answers2

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;
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Can you add methods to your object and still store it as a session variable? – user1780242 Aug 15 '14 at 00:34
  • 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](http://php.net/manual/en/language.oop5.serialization.php) – RiggsFolly Aug 15 '14 at 00:38
  • 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(); – user1780242 Aug 15 '14 at 00:41
  • 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. – RiggsFolly Aug 15 '14 at 00:44
  • You might try `array_diff($_SESSION['cart'][], $items)` that might work. – RiggsFolly Aug 15 '14 at 00:51
  • I tried the print_r like you suggested above and it throws the same array to string conversion error. – user1780242 Aug 15 '14 at 00:56
  • And if I do `print_r(array_diff($_SESSION['cart'][], $items));`I get an error that says Fatal Error: Cannot use [] for reading in... – user1780242 Aug 15 '14 at 01:00
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);