Discuss about multi-key cookie (cookie Access array in php)

Source: Internet
Author: User
This article provides a detailed analysis of multi-key cookie (cookie Access array in php). For more information, see

This article provides a detailed analysis of multi-key cookie (cookie Access array in php). For more information, see

By default, Cookies cannot store arrays and website spaces. Therefore, the following statements are incorrect.
The following error is reported:
Warning: setcookie () expects parameter 2 to be string, array given in
However, PHP can resolve the cookie with the same name and ending with [] to an array. In php, the method to implement the cookie storage array is as follows:

Method 1: First serialize the array with serialize and then store the COOKIE. Then, use unserialize to get the original array.

Method 2: Set the multi-key cookie, website space, and Hong Kong Space.

The Code is as follows:


$ Arr = array (1, 2, 3 );
Setcookie ("a [0]", $ arr [0]);
Setcookie ("a [1]", $ arr [1]);
Setcookie ("a [2]", $ arr [2]);


Result: All elements in the array are saved.
Array length: 3
Array ([0] => 1 [1] => 2 [2] => 3)

The following statements are incorrect:

The Code is as follows:


$ Arr = array (1, 2, 3 );
Setcookie ("a []", $ arr [0]);
Setcookie ("a []", $ arr [1]);
Setcookie ("a []", $ arr [2]);


Result: only the last element is saved.
Array length: 1
Array ([0] => 3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.