0

I have an array to store items id and quantity for a shopping cart which is stored in the session.

$_SESSION['cart'][] = array('shoeId' => $shoeID, 'quantity' => $quantity); 

However if the user were to add a duplicated item ID I am trying to look into the array for that value and increase the quantity number accordingly and this is what I am using now but it keeps throwing out the notice undefined offset

//create a new array
$cleanArray = array();

//store the session cart array into a variable.
$cartArray = $_SESSION['cart'];

//Iterate through the Array stored in the session and if the ID exist then add the quantity from the $cartArray to the $CleanArray. Else add a new item to the $cleanArray.
    foreach($cartArray as $item){
        if($cleanArray[$item['shoeId']]){
            $cleanArray[$item['shoeId']]['quantity'] += $item['quantity'];
        } else {
            $cleanArray[$item['shoeId']] = $item;
        }

    }

The notice is thrown out at the foreach($cartArray as $item). It would always throw out the notice followed by the offset of the shoeId number. I am quite new to PHP and don't know how to solve this. I would be thankful for any suggestion and an explanation to this notice as I want to learn more.

Lee Chun Yong
  • 11
  • 1
  • 6

0 Answers0