how to use and save php array in cookie using serialize and unserialize

Hello friends,

As an php developer , many times you may need to store php array in cookies. Am i right ?

If you are creating any shopping cart, then you may have requirement to store billing and shipping address in cookies.  To minimize code we like to store billling and shipping address in array. But you may have face problem about how to store php array in cookies. I had also felt same problem. After spend some times, i have founded a very easy steps. Perhaps you may like it.  Many of you had already tried serialize and unserialize php array  in cookies, but may be failure to get perfect results.

How to store php array in cookies :

$phparray=array(“name”=>”alex”,”phone”=>”123456789″);
setcookie(‘mycookiename’, serialize($array) ); 

Just convert php array into serialize as above to store in cookie

To get back php array from cookie, just use given below code

$data = $_COOKIE[‘mycookiename’];
$data= html_entity_decode($data);    // Important
$myarray= unserialize($data);

Output of $myarray and $phparray will be same. You dont require jsonencode or decode. 

Is it not easy solutions ? !!