问题
I'm trying to store an array for cookie, but I get the following error:
Warning: setcookie() expects parameter 2 to be string, array given
How can I actually put an array to a cookie?
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.
Example. first submission array[1206,0402], second submission array[0402]. but the outcome is then [0402,0402] which is 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] as $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: The question has been resolved. The 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] as $key =>$trash)
setcookie("{$var}[".$key.']', '', time()-60*60*24*365);
if (is_array($_POST[$var]))
foreach ($_POST[$var] as $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();
}
回答1:
Answer
You can store cookies using array syntax and read them as a multi-dimensional arrays:
setcookie('array[key]', 'value');
$var = $_COOKIE['array']['key'];
Your code would look like this:
for($val as $key=>$value)
setcookie('vals['.$key.']', $value, time()+60*60*24*365);
Multi-Dimensional Arrays
You can also store multi-dimensional arrays the same way:
setcookie('array[key1][key2]', 'value');
$var = $_COOKIE['array']['key1']['key2'];
Clearing the Cookie
When you need to clear out the cookie, there are multiple methods; the longest being:
for($_COOKIE['array'] as $key=>$value)
setcookie('array['.$key.']', '', time()-60*60*24*365);
The easiest and most preferable way is this:
setcookie('array', '', time()-60*60*24*365);
Conclusion
Cookies allow arrays to be stored using standard array syntax. Storing a multi-dimensional array is also standard syntax.
To destroy a cookie with an array value, use the same syntax as for a normal cookie, either over the whole array or on each specific element.
The documentation on setcookie() goes over this.
回答2:
An alternative solution would be to use serialize() and unserialize to store cookie data in one cookie, or json_encode(). Although standard array syntax behaves like an array, you are actually setting multiple cookies to make it work, which seems kind of wasteful. So, to use PHP documentation's own example
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");
//On next pageload $_COOKIE['cookie'] = array ('one' => 'cookieone', 'two'=>'cookietwo', 'three' => 'cookiethree');
You have just set 3 cookies.
$array = array('value1', 'value2', 'value3');
setcookie('cookie', serialize($array);
//On next pageload
$array = unserialize($_COOKIE['cookie']);
You have set one cookie instead of three with the same data.
来源:https://stackoverflow.com/questions/9657490/how-to-put-a-cookie-into-an-array