I store an array of values in my cookie as a string (with ',' as separator). I update them using explode(), implode() and setcookie() methods in a custom function set_Cookie() and it works great.
function set_Cookie($name, $position, $value) {
$cookie = ($_COOKIE[$name]);
$cookie_exp = explode(",", $cookie);
$cookie_exp[$position] = $value;
$cookie_imp = implode(",", $cookie_exp);
setcookie($name,$cookie_imp);
}
The only problem I have is when I try to call the function multiple times - only the last call succeeds in updating the value. In other words: In the code below only 'position3' would get updated with 'value3' but other positions would not get updated at all:
set_Cookie('cookie1','$position1','value1');
set_Cookie('cookie1','$position2','value2');
set_Cookie('cookie1','$position3','value3');
Initial cookie1 values: 0,0,0
Result: 0,0,value3
What am I missing?