Cookies cannot store arrays by default, so the following writing is wrong.
The error reported is as follows:
Warning: setcookie() expects parameter 2 to be string, array given in
But PHP can put the same name and end with [] The cookie is parsed as an array. The method to implement cookie storage in php is as follows:
Method 1: Use serialize to serialize the array first, then store it in COOKIE. When reading it out, use unserialize to get the original array
Method 2: Set multi-key values cookie, note that the key value must be
Copy code 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 of the array are saved.
Array length: 3
Array ( [0] => 1 [1] => 2 [2] => 3 )
The following writing method is
Error:
Copy code 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 )
http://www.bkjia.com/PHPjc/327414.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327414.htmlTechArticleCookies cannot store arrays by default, so the following writing is wrong. The error is as follows: Warning: setcookie() expects parameter 2 to be string, array given in But PHP can put the same name and the last...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn