Rumah > Artikel > pembangunan bahagian belakang > php及JS操作 cookie保存数组方法
如何把二维数组存储到cookie中呢?
先把数组转换成字符串,要用时再去掉斜线(为什么会出现斜线呢?)转换回来
a.php
<? $cart_info[0][0] = "1123"; $cart_info[1][0] = "5334521"; $cart_info[1][2] = "df"; $cart_info[4][2] = "fefe"; setcookie("xia",serialize($cart_info)); ?>
b.php
<? $other = StripSlashes($_COOKIE['xia']); print_r(unserialize($other)); ?>
// js 创建cookie
// var name = "str1"; var function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值 { var Days =30; //此 cookie 将被保存 30 天 varexp = newDate(); //newDate("December 31, 9998"); exp.setTime(exp.getTime() + Days*24*60*60*1000); document.cookie = name + "="+ escape (value) + ";expires=" +exp.toGMTString(); } functiongetCookie(name)//取cookies函数 { var arr =document.cookie.match(new RegExp("(^|)"+name+"=([^;]*)(;|$)")); if(arr != null) return (arr[2]); return null; } function delCookie(name)//删除cookie { var exp =new Date(); exp.setTime(exp.getTime() - 1); varcval=getCookie(name); if(cval!=null) document.cookie= name +"="+cval+";expires="+exp.toGMTString(); } SetCookie ("xiaoqi", "3")