[英]unserialize() function does not work with more than one index using php
I'm storing data in cookie using serialize/unserialize function. 我使用序列化/反序列化功能将数据存储在cookie中。 Unserialize function does not working when i add new item to array. 当我向数组添加新项时,反序列化功能不起作用。
$storedArr = array();
if(isset($_REQUEST['sendProductId'])){
$newItem = $_REQUEST['sendProductId'];
$storedArr[] = $_COOKIE['productID'];
array_push($storedArr, $newItem);
$cookie_name = 'productID';
setcookie($cookie_name, serialize($storedArr), time() + (86400 * 30));
}
$cookieData = $_COOKIE['productID'];
$data = unserialize($cookieData);
print_r($data);
Array ( [0] => [1] => 50 )
Array ( [0] => a:2:{i:0;N;i:1;s:2:"50";} [1] => 50 )
Please guide me where i'm wrong. 请指导我我错了。 Thanks 谢谢
i see logical issue in your code, when you get data from cookie as it is serialized you have to first unserialize it then use 我在您的代码中看到了逻辑问题,当您从cookie进行序列化获取数据时,必须先对其进行反序列化,然后再使用
$storedArr[] = $_COOKIE['productID'];
change to 改成
$storedArr = !empty($_COOKIE['productID']) ? unserialize( $_COOKIE['productID'] ):array();
it should solve your issue. 它应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.