I need to set array as a value of cookie, and here I found reasonable solution: Arrays in cookies PHP. So here is what I have tried:
<?php
$array=array();
foreach($columnList as $c_list) {
foreach($c_list as $one_member) {
echo '<button class="btn btn-default" type="button"><b>' . $one_member . '</b></button>';
array_push($array,$one_member);
}
}
setcookie('new_column',serialize($array), time() + (10 * 365 * 24 * 60 * 60));
?>
Note that I am looping through two-dimensional array and pushing each single value into array called $array
. But when I do this :
print_r($_COOKIE['new_column']);
It displays only the first value, i.e. the first $one_member
. And when I do this:
$data=unserialize($_COOKIE['new_column']);
print_r($data);
It basically displays nothing. What am I doing wrong?
UPDATE: I found what is the pre-issue of this issue in question, and here it is: I tried putting just this:
foreach($c_list as $one_member) {
echo '<button class="btn btn-default" type="button"><b>' . $one_member . '</b></button>';
$array=array();
$array[]=$one_member;
}
Without array_push
and this returns just last $one_member
. So I first need to fix this in order to put adequate array into cookie, but I don't even know why this occurs.