0

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.

Community
  • 1
  • 1
Ognjen Babic
  • 131
  • 16
  • 1
    Try this : http://stackoverflow.com/questions/19068363/storing-and-retrieving-an-array-in-a-php-cookie – Mr. Engineer Feb 25 '16 at 07:51
  • Is it `setcookie()` that does not seem to work or the array filling? You can check by printing `$array` and/or `serialize($array)` 's result. – RhinoDevel Feb 25 '16 at 07:53
  • It seems like `array_push` is not working, because when I `print_r($array)` it displays nothing. – Ognjen Babic Feb 25 '16 at 08:00

0 Answers0