This article provides a detailed analysis of multi-key cookie (cookie Access array in php). For more information, see
This article provides a detailed analysis of multi-key cookie (cookie Access array in php). For more information, see
By default, Cookies cannot store arrays and website spaces. Therefore, 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, website space, and Hong Kong Space.
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)
The following statements are incorrect:
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)