title: how to put a Cookie into an array?
body: <p>I'm trying to store an array for cookie, but I get the following error:</p>
  
  <p><strong>Warning: setcookie() expects parameter 2 to be string, array given</strong></p>
  
  <p>How can I actually put an array to a cookie? </p>
  
  <p>EDIT2: I Edited the code, and I it does store cookie array now, but I've got a HUGE problem though. It does not override the values from first submission if the size of second array is smaller.</p>
  
  <p>Example. first submission array[1206,0402], second submission array[0402]. but the outcome is then [0402,0402] which is wrong.</p>
  
  <pre><code>function cuukko($var,$val){
      setcookie($var,$val,time()+60*60*24*365);
  }
  
  function preg_DW($var){
      global $isset;
      if ($isset&amp;&amp;is_array($_POST[$var])&amp;&amp;sizeof($_POST[$var])&gt;0){
          $C=0;
          foreach ($_POST[$var] as $key =&gt; $value) {
              $val[$C]=trim(preg_replace('/\s\s+/',' ',preg_replace('/[^\d\w\s\(\)\[\]]+/','',$value)));
              cuukko($var."[".$C."]",$val[$C]);
              $C++;
          }
      } elseif (isset($_COOKIE[$var])) $val=$_COOKIE[$var];
          return (sizeof($val)&gt;0)?$val:array();
  }
  </code></pre>
  
  <p>Edit 3: The question has been resolved. The code in use now:</p>
  
  <pre><code>function cuukko($var,$val){
      setcookie($var,$val,time()+60*60*24*365);
  }
  
  function preg_DW($var){
      global $isset;
      if ($isset){
          $C=0;
          if (is_array($_COOKIE[$var]))
          foreach($_COOKIE[$var] as $key =&gt;$trash)
          setcookie("{$var}[".$key.']', '', time()-60*60*24*365);
          if (is_array($_POST[$var]))
          foreach ($_POST[$var] as $key =&gt; $value) {
              $val[$C]=trim(preg_replace('/\s\s+/',' ',preg_replace('/[^\d\w\s\(\)\[\]]+/','',$value)));
              cuukko($var."[".$C."]",$val[$C]);
              $C++;
          }
      } elseif (isset($_COOKIE[$var])) $val=$_COOKIE[$var];
          return (sizeof($val)&gt;0)?$val:array();
  }
  </code></pre>