Reputation: 5870
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.
Upvotes: 1
Views: 704
Reputation: 5870
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
Upvotes: 0
Reputation: 1347
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;
}
Upvotes: 1
Reputation: 1352
You can use json_encode($random_ads) or just join('|', $random_ads) and then parse it in on the client-side.
Upvotes: 0