Tags
Asked 3 years ago
9 Jun 2021
Views 280
eclipse-learner

eclipse-learner posted

Arrays in cookies PHP

Arrays in cookies PHP
yogi

yogi
answered Apr 26 '23 00:00

n PHP, you can store arrays in cookies by serializing the array and then setting it as the cookie value. Here is an example of how to do it:



// Create an example array
$my_array = array('apple', 'banana', 'orange');

// Serialize the array
$serialized_array = serialize($my_array);

// Set the cookie with the serialized array
setcookie('my_cookie', $serialized_array, time() + 3600);

To retrieve the array from the cookie, you would first get the serialized string from the cookie value, and then unserialize it back into an array:



// Get the serialized array from the cookie
$serialized_array = $_COOKIE['my_cookie'];

// Unserialize the array
$my_array = unserialize($serialized_array);

// The array can now be used
echo $my_array[0]; // Output: apple
Post Answer