By default, the cookie cannot store arrays, so 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.
Copy codeThe 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)
Which of the following statements is as follows:ErrorOf:
Copy codeThe 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)