Why are cookies saving an incremented value of variable passed to them?
I am doing all my cart processing for an ecommerce website on a page and storing it's result in an array by the name $cart [ an associative array ] containing the ID's of items as the key and the quantity of items as the value.Then i am storing the cart array into a session variable as well as the cookies, so they are updated on every page.

Now i'd like to tell you guys that at Point 1, i have checked the value of $value and it comes out to be 1 [ whenever a single item is added ], but right after that step the cookie is set and if I check it's value in the developer panel, it is specified as 2 ! Now this is creating a problem for me because i want a value of just '1' to be stored but it's being stored in cookie as '2' ??? I am using the following code for keeping the SESSION & COOKIES updated:

    if(isset($_SESSION['cart'])) {                    
         $cart=$_SESSION['cart'];
         foreach ($cart as $key => $value) {
             // Point1 - Right before cookie is set, $value is still 1
             setcookie("cartcookie[$key]", "$value", time()+(60*60*24*7*365*100));
             // Point 2 - As soon as a cookie is set, $value [ in the $cart array ] still              remains '1' but it's value in cookie is shown as '2'.
       }
    }
    elseif(isset($_COOKIE)&&!isset($_SESSION['cart'])){
     $cart=array();
     foreach ($_COOKIE as $key => $value) {
          if ($key=='cartcookie') {
               foreach ($value as $k => $v) {
                    $cart[$k]=$v;
               }
          }
     }
     $_SESSION['cart']=array();
     $_SESSION['cart']=$cart;
    }

This is causing problem for me because when a user has added something to his/her cart and then closed the browser, the cart array [ $cart ] remains stored in COOKIES. So the next time he/she visits, the cart array is updated into a SESSION variable. In this scenario, a wrong value of '2' in place of '1' can be a huge problem for me. Can anybody help me out here ?
Why are cookies saving an incremented value of variable passed to them?
===

I am doing all my cart processing for an ecommerce website on a page and storing it's result in an array by the name $cart [ an associative array ] containing the ID's of items as the key and the quantity of items as the value.Then i am storing the cart array into a session variable as well as the cookies, so they are updated on every page.

Now i'd like to tell you guys that at Point 1, i have checked the value of $value and it comes out to be 1 [ whenever a single item is added ], but right after that step the cookie is set and if I check it's value in the developer panel, it is specified as 2 ! Now this is creating a problem for me because i want a value of just '1' to be stored but it's being stored in cookie as '2' ??? I am using the following code for keeping the SESSION & COOKIES updated:

    if(isset($_SESSION['cart'])) {                    
         $cart=$_SESSION['cart'];
         foreach ($cart as $key => $value) {
             // Point1 - Right before cookie is set, $value is still 1
             setcookie("cartcookie[$key]", "$value", time()+(60*60*24*7*365*100));
             // Point 2 - As soon as a cookie is set, $value [ in the $cart array ] still              remains '1' but it's value in cookie is shown as '2'.
       }
    }
    elseif(isset($_COOKIE)&&!isset($_SESSION['cart'])){
     $cart=array();
     foreach ($_COOKIE as $key => $value) {
          if ($key=='cartcookie') {
               foreach ($value as $k => $v) {
                    $cart[$k]=$v;
               }
          }
     }
     $_SESSION['cart']=array();
     $_SESSION['cart']=$cart;
    }

This is causing problem for me because when a user has added something to his/her cart and then closed the browser, the cart array [ $cart ] remains stored in COOKIES. So the next time he/she visits, the cart array is updated into a SESSION variable. In this scenario, a wrong value of '2' in place of '1' can be a huge problem for me. Can anybody help me out here ?