This article describes how to use a multi-key cookie (cookie access array in php. By default, the cookie cannot store arrays, so the following statements are incorrect. The following error is reported: Warning: setcookie () expectsparameter2tobestring, arraygiv.pdf. However, PHP can save an array with the same name and no cookie can be saved by default. Therefore, the following statement is 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)
Bytes. The following error is reported: Warning: setcookie () expects parameter 2 to be string, array given in, but PHP can put the same name and later...