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)