Hi All

I am a bit of a begginer in PHP.
How do I store multiple vales in a cookie?
I am going along the lines of:

$data = array('apple'=>'green',		'orange'=>'orange',
	'banana'=>'yellow');

then something like (not sure about the sytax here)::

$var=implode($data);
setcookie('data', $var, time() +3600);

Basically how do I get the information into and out of the cookie.

Thanks in advance for any help.

Hi,

Why don't you concatenate values in a string like

apple,orange,mango
 and then store it in cookie.

I really need it to be an associative array that is stored in the cookie and then be able to output the relevant data. If that makes any sense.

Well if you want to implode the values and store it in the cookie then you'd explode it to retrieve it as an array.

$array = array('orange', 'green', 'blue');
$string = implode(',', $array); // 'orange,green,blue'
$second_array = explode(',', $string); // array('orange', 'green', 'blue');

Thanks for all the help.
I know how to do the array part but I cannot get it into my head how to put the array into a single cookie and then get the data back out of the cookie.
I have spent most of yesterday googling for a solution with no real luck.

Thanks again in advance.

Well you did it in your example. All I did was explain the correct syntax for implode/explode. Also, see www.php.net/cookies

Thanks
but I am not sure on how to get the data back out of the cookie.

Thanks
but I am not sure on how to get the data back out of the cookie.

Then you didn't read the link I posted :)

Hi
Thanks again for all the help, my head is going round and round as I have been at thsi for so long now. i right in saying to set the cookie I :

$data = array('apple'=>'green', 'orange'=>'orange',
'banana'=>'yellow');
$var=implode( ',', $data);
setcookie('data', $var, time() +3600);

and that has set the cookie?
I know how to set and then get everything out of a normal cookie
I alsoknw how to use the array but put them both together and I have a total block. I just cannot get my head round getting the data from within the array that is stored in the cookie. I have read your link but it is not helping me as I think my brain is fried.

Any help very very gratefully accepted.

If you know how to set and get data from the cookie I just gave you how to use implode/explode, just put them together. You already did the first part in your original post.

Hi
Thanks for the help I am nearly there but not quite.
I have set the cookie:

$data = array('apple'=>'green', 'orange'=>'orange',
'banana'=>'yellow');
$var=implode( ',', $data);
setcookie('fred', $var, time() +3600);

And to print it out I now have:

//print a cookie
$list=explode(',',$_COOKIE['fred']);
echo $list['0'];
echo $list['1'];
echo $list['2'];
echo '<br />';
echo '<br />';
echo '<br />';
foreach ($list as $key => $value) {
echo "$key => $value <br />";
}

foreach ($list as $i =>$j)
{
echo "<p>An{$i} is {$j}. <br />";
}

This unfortunatley only gives me the value and not the key as below:

greenorangeyellow


0 => green
1 => orange
2 => yellow

An0 is green.

An1 is orange.

An2 is yellow.

How do I get it to print out the keys?
Thanks in advance

use serialize and unserialize instead of implode/explode

Bother thought I was nearly there!

Now totally lost.
I have tried:

$data = array('apple'=>'green', 'orange'=>'orange',
'banana'=>'yellow');
$var=serialize($data);
print serialize($data);
$next=unserialize($var);
echo $next;

and I get
a:3:{s:5:"apple";s:5:"green";s:6:"orange";s:6:"orange";s:6:"banana";s:6:"yellow";}Array I got there

Thanks for anhep in advance

That is correct, you're not lost. You get

a:3:{s:5:"apple";s:5:"green";s:6:"orange";s:6:"orange";s:6:"banana";s:6:"yellow";}

from print serialize($data); and you get Array from echo $next As a side note, don't mix echo and print, there's no reason to, they do the same thing.

Thanks again for all the help I really appreciate all the help I am getting, but now how do I get this into a cookie back out I seem to be tying myself up in knots!

heh, I guess I'm just not understand how you're not getting it. You can do 4 + 0 = 4 and you can do 0 + 4 = 4 but for some reason you can't do 2 + 2 = 4. You have the two pieces of the puzzle, you're just not putting them together.

Set the cookie

$some_array = array('Hello' => 'World');
setcookie('some_cookie', serialize($some_array), time() +3600);

Retrieve the cookie

$some_array = unserialize($_COOKIE['some_cookie']);

Hi
Thanks for all your help I had few hours break went over it again and now have everything displaying properly both keys and value yipeee!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.