title: How to add / edit a cookie in php?
body: <p>I'm using the following functions to set an array of values in a cookie in PHP, but I also need an "add" and "edit" function - any suggestions on how I can do that?</p>
  
  <pre><code>function build_cookie($var_array) {
    if (is_array($var_array)) {
      foreach ($var_array as $index =&gt; $data) {
        $out.= ($data!="") ? $index."=".$data."|" : "";
      }
    }
    return rtrim($out,"|");
  }
  
  function break_cookie ($cookie_string) {
    $array=explode("|",$cookie_string);
    foreach ($array as $i=&gt;$stuff) {
      $stuff=explode("=",$stuff);
      $array[$stuff[0]]=$stuff[1];
      unset($array[$i]);
    }
    return $array;
  }
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>setcookie("mycookies", build_cookie($cookies_array), time()+60*60*24*30);
  
  $cookies_array2 = break_cookie(urldecode($_COOKIE['mycookies']));
  
      foreach ($cookies_array2 as $k =&gt; $v) {
          echo "$k : $v &lt;br /&gt;\n";
      }
  </code></pre>