php - how to put a Cookie into an array? -


i'm trying store array cookie, following error:

warning: setcookie() expects parameter 2 string, array given

how can put array cookie?

edit2: edited code, , store cookie array now, i've got huge problem though. not override values first submission if size of second array smaller.

example. first submission array[1206,0402], second submission array[0402]. outcome [0402,0402] wrong.

function cuukko($var,$val){     setcookie($var,$val,time()+60*60*24*365); }  function preg_dw($var){     global $isset;     if ($isset&&is_array($_post[$var])&&sizeof($_post[$var])>0){         $c=0;         foreach ($_post[$var] $key => $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)>0)?$val:array(); } 

edit 3: question has been resolved. code in use now:

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] $key =>$trash)         setcookie("{$var}[".$key.']', '', time()-60*60*24*365);         if (is_array($_post[$var]))         foreach ($_post[$var] $key => $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)>0)?$val:array(); } 

answer

you can store cookies using array syntax , read them multi-dimensional arrays:

setcookie('array[key]', 'value'); $var = $_cookie['array']['key']; 

your code this:

for($val $key=>$value)   setcookie('vals['.$key.']', $value, time()+60*60*24*365); 


multi-dimensional arrays

you can store multi-dimensional arrays same way:

setcookie('array[key1][key2]', 'value'); $var = $_cookie['array']['key1']['key2']; 


clearing cookie

when need clear out cookie, there multiple methods; longest being:

for($_cookie['array'] $key=>$value)   setcookie('array['.$key.']', '', time()-60*60*24*365); 

the easiest , preferable way this:

setcookie('array', '', time()-60*60*24*365); 


conclusion

cookies allow arrays stored using standard array syntax. storing multi-dimensional array standard syntax.

to destroy cookie array value, use same syntax normal cookie, either on whole array or on each specific element.

the documentation on setcookie() goes on this.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -