0

i want to store an array of strings in a PHP cookie example :

$array = array("1", "test");
setcookie($cookie_name, $array, time() + (86400 * 30), "/");
CoderTn
  • 985
  • 2
  • 22
  • 49

2 Answers2

2
$array = array('a'=>"1", 2=>"test", 25);
$json = json_encode($array);
setcookie('name', $json);
array = json_decode($_COOKIE['name']);
TsV
  • 629
  • 4
  • 7
1

You can parse it into a string (e.g. http_build_query($array) or serialize) and store it in the cookie and then again to read from it you can parse the string (e.g. str_parse($_Cookie['cookie']) or deserialize).

MIIB
  • 1,849
  • 10
  • 21
  • what means to parse and serialize please ? – CoderTn Jul 25 '18 at 13:47
  • 1
    There are some methods to convert an array to a string. http_build_query or serialize are methods to convert this. Then you want to convert the string back to an array which you can user str_parse if you converted the array using http_build_query or unserialize if you've used serialize method. $array = array("1", "test"); setcookie($cookie_name, serialize($array), time() + (86400 * 30), "/"); $array = unserialize($_COOKIE['cookie_name']); – MIIB Jul 25 '18 at 13:51
  • thanks , that's helpful :) – CoderTn Jul 26 '18 at 09:46