A detailed _php tutorial on multi-key cookie (cookie access array in PHP)

Source: Internet
Author: User
Tags setcookie
The cookie cannot be stored in an array by default, so the following notation is incorrect.
The error is as follows:
Warning:setcookie () expects parameter 2 to is string, array given in
But PHP can parse the array with the same name and the cookie that ends with []. The following methods are implemented in PHP to store arrays in cookies:

Method One: First use serialize serialization array, and then deposit the cookie, read out with Unserialize to get the original array

Method Two: Set the multi-key value cookie, note must give the key value
Copy the Code code as follows:
$arr = Array (+/-);
Setcookie ("a[0]", $arr [0]);
Setcookie ("a[1]", $arr [1]);
Setcookie ("a[2]", $arr [2]);

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

The following wording is incorrect :
Copy the Code code as follows:
$arr = Array (+/-);
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)

http://www.bkjia.com/PHPjc/327414.html www.bkjia.com true http://www.bkjia.com/PHPjc/327414.html techarticle The cookie cannot be stored in an array by default, so the following notation is incorrect. The error is as follows: Warning:setcookie () expects parameter 2 to is string, array given in but PHP can put the same name and later ...

  • 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.