Ami*_*mit 2 php arrays cookies store setcookie
我通过 php 序列化函数将数组转换为 cookie
$PromoteuserId='1';
$PromoteProductId='2';
$PromoteBrandId='3';
$PromoteProductArray = array("PromoteuserId"=>$PromoteuserId,
"PromoteProductId"=>$PromoteProductId,
"PromoteBrandId"=>$PromoteBrandId
);
$Promotedcart[] = $PromoteProductArray;
setcookie("Promotedcart", urlencode(serialize($Promotedcart)), time()+604800,'/');
Run Code Online (Sandbox Code Playgroud)
创建 cookie 后,我将使用反序列化 php 函数。
print_r(unserialize(urldecode($_COOKIE['Promotedcart'])));
Run Code Online (Sandbox Code Playgroud)
我需要更新 cookie 值。例如 - 我需要在 cookie 中搜索 PromotionProductId 值,如果那么它会将 cookie 值更新为与 PromotionProductId 一致。可以指导我如何更新该值吗?
您可以简单地将未序列化的 cookie 存储到变量中,然后重置 cookie 吗?
$array = unserialize(urldecode($_COOKIE['Promotedcart']));
$array[0]["PromoteuserId"] = "New";
setcookie("Promotedcart", urlencode(serialize($array)), time()+604800,'/');
Run Code Online (Sandbox Code Playgroud)