如何将Cookie放入数组? [英] how to put a Cookie into an array?

查看:404
本文介绍了如何将Cookie放入数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图存储一个数组的cookie,但我得到以下错误:

I'm trying to store an array for cookie, but I get the following error:

警告:setcookie()期望参数2是字符串,array given

如何实际将数组放入cookie?

How can I actually put an array to a cookie?

EDIT2:我编辑了代码,我现在存储cookie数组,但是我有一个很大的问题。如果第二个数组的大小较小,它不会覆盖第一次提交的值。

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.

示例。第一提交数组[1206,0402],第二提交数组[0402]。但结果是[0402,0402]错了。

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();
}

编辑3:问题已解决。现在使用的代码:

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();
}


推荐答案

b
$ b

您可以使用数组语法存储Cookie,并将其读为多维数组:

Answer

You can store cookies using array syntax and read them as a multi-dimensional arrays:

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

您的代码将如下所示:

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


您也可以以同样的方式存储多维数组:

You can also store multi-dimensional arrays the same way:

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


当您需要清除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);


Cookie允许使用标准数组语法存储数组。存储多维数组也是标准语法。

Cookies allow arrays to be stored using standard array syntax. Storing a multi-dimensional array is also standard syntax.

要销毁带有数组值的cookie,请使用与普通cookie相同的语法,在整个数组或每个特定元素。

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.

setcookie() 的文档。

这篇关于如何将Cookie放入数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆