To explore the detailed _php techniques for multi-key value cookies (cookie access arrays in PHP)

Source: Internet
Author: User
Tags array length setcookie

Cookies cannot save an array by default, so the following is the wrong notation.
The error is as follows:
Warning:setcookie () expects parameter 2 to is string, array given in
However, PHP can parse an array with a cookie with the same name and a trailing end. The way to implement a cookie-stored array in PHP is as follows:

Method One: First, use serialize to serialize the array, then save the cookie, and read it with Unserialize to get the original array.

Method Two: Set a multiple-key value cookie, note must give the key value

Copy Code code as follows:

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

Results: all elements of the array are saved.
Array Length: 3
Array ([0] => 1 [1] => 2 [2] => 3)

The following wording is ErrorOf
Copy Code code as follows:

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

Results: only one last element was 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.