I am trying to increase quantity of a product when the id of that product already exists inside an array.
My product array $arr2 looks like this for example:
Array
(
[productid] => 3
[productname] => Eikenhout pallet
[productalias] => eikenhout-pallet
[productcat] => Eikenhout
[catalias] => eikenhout
[quantity] => 1
[kuubkosten] => 150|1 kuub
)
Then I create a session and add all information to it.
if (!isset($_SESSION['cart'])) {
// and turn session into array
$_SESSION['producten'] = array();
}
// If id of product does not exist, add it to session
if(!isset($_SESSION['producten'][$arr2['productid']])){
$_SESSION['producten'][$arr2['productid']] = $arr2;
// Else add to quantity
}else{
$_SESSION['producten'][$arr2['productid']]['quantity'] += $arr2['quantity'];
}
If I then print my session, this is what I get:
Array
(
[3] => Array
(
[productid] => 3
[productname] => Eikenhout pallet
[productalias] => eikenhout-pallet
[productcat] => Eikenhout
[catalias] => eikenhout
[quantity] => 1
[kuubkosten] => 150|1 kuub
)
)
But after adding the product again, the quantity is not increased. What am I doing wrong? I can see in my network tab that all data is correctly posted and there are no errors.
+= $arr2['quantity']should be$arr2['quantity']+1;maybe+=is an acceptable operator in PHP..$_SESSION['producten'][$arr2['productid']] = $arr2;then WHERE in the session array isproducten$_SESSION['producten']shows me the second array in my question.