Get the App
SLTechnology News&Howtos  ›  Development  › 

How to access Array by cookie in php

Shulou Source: shulou.com Published: 2022-06-03 06:14:01 11月16日 Update

This article mainly introduces the php cookie how to access the array, the article is very detailed, has a certain reference value, interested friends must read it!

Cookie cannot store arrays by default, so the following is incorrect.

The error is as follows:

Warning: setcookie () expects parameter 2 to be string, array given in

But PHP can parse cookie with the same name followed by [] into an array. The method to implement cookie memory array in php is as follows:

Method 1: first serialize the array with serialize, then store it in COOKIE, and read it out with unserialize to get the original array.

Method 2: set the multi-key value cookie, pay attention to the key value must be given

The copy code is as follows:

$arr = array (1pm 2pm 3)

Setcookie ("a [0]", $arr [0])

Setcookie ("a [1]", $arr [1])

Setcookie ("a [2]", $arr [2])

The result: all the elements of the array are stored in.

Array length: 3

Array ([0] = > 1 [1] = > 2 [2] = > 3)

The following words are incorrect:

$arr = array (1pm 2pm 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)

These are all the contents of the article "how to access arrays in cookie in php". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Array method access code element content writing article result error length value interest partner sequence more knowledge industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Huawei MariaDB NVidia MySQL