This article provides a detailed analysis of multi-key-value cookies (the cookie access array in php). For more information about how to save an array by default, 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.
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)
Which of the following statements is as follows:ErrorOf:
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)