I want to shuffle an array then serialized it and save it as a cookie.
$random_ads = array(
'<li><a href="#">Test 1</a></li>',
'<li><a href="#">Test 2</a></li>',
'<li><a href="#">Test 3</a></li>'
);
shuffle($random_ads);
$ra_serialized = serialize($random_ads);
setcookie('random_ads', $ra_serialized, time()+3600*24, '/');
Then I tried to use unserialized($_COOKIE['random_ads']) and print the array, but did not work. It print nothing. Please give me some ideas. The main thing I want is to save the array in cookie and get it back when I want. Thanks.
try this
$random_ads = array(
'<li><a href="#">Test 1</a></li>',
'<li><a href="#">Test 2</a></li>',
'<li><a href="#">Test 3</a></li>'
);
shuffle($random_ads);
$ra_serialized = serialize($random_ads);
setcookie('random_ads', $ra_serialized, time()+3600*24, '/');
$getarray = unserialize($_COOKIE['random_ads']);
foreach($getarray as $getarray1)
{
echo $getarray1;
}
您可以使用json_encode($ random_ads)或仅加入('|',$ random_ads),然后在客户端进行解析。
After browsing for some hours, I found this solution and it works!
//to safely serialize
$safe_string_to_store = base64_encode(serialize($multidimensional_array));
//to unserialize...
$array_restored_from_db = unserialize(base64_decode($encoded_serialized_string));
Read more about this solution HERE
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.