本篇文章是对多键值cookie(php中cookie存取数组)进行了详细的分析介绍,需要的朋友参考下
cookie默认不能存数组,网站空间,所以下面的写法是错误的。
报错如下:
Warning: setcookie() expects parameter 2 to be string, array given in
但是PHP可以把同名且后面以[]结尾的cookie解析为数组。在php里面实现cookie存数组的方法如下:
方法一:先用serialize序列化数组,再存入COOKIE ,读出来时用unserialize得到原来的数组
方法二:设定多键值cookie,网站空间,香港空间,注意必须给键值
复制代码 代码如下:
$arr = array(1,2,3);
setcookie("a[0]", $arr[0]);
setcookie("a[1]", $arr[1]);
setcookie("a[2]", $arr[2]);
结果:数组所有元素都存到了。
数组长度 :3
Array ( [0] => 1 [1] => 2 [2] => 3 )
以下写法是错误的:
复制代码 代码如下:
$arr = array(1,2,3);
setcookie("a[]", $arr[0]);
setcookie("a[]", $arr[1]);
setcookie("a[]", $arr[2]);
结果:只存了最后一个元素
数组长度 :1
Array ( [0] => 3 )

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor