Start New Discussion within our Web Development Community

i am trying to put all of my cookies into an array. heres my code

setcookie('failn','fail'); setcookie('goodn','good'); setcookie('han','ha');

$allcookies=explode(' ',$_COOKIE);


but the page only returns

Array ( [0] => Array )

please help



if array is still empty then it means for some reason cookie didnt set.

The array is already split. What you're asking doesn't make any sense.

$_COOKIE is an array
e.g. array( key => value, key => value, key => value)

Explode is meant to take a string like "this is a test" and explode it into
array( 0 => this, 1 => is, 2 => a, 3 => test )

If you're trying to turn the array into a string, you want implode.

$string = implode( ' ', $_COOKIE );
//value value1 value2 value3 value4

now how would i do a for loop to search for a certain cookie, and then put that cookie value into a variable?

Can you give me a better example of what it is you have and what you're trying to find through a loop? Maybe some code that you're trying to get to work? setcookie('TestCookie', $value) would be read back with $_COOKIE

There isn't a reason to loop over the cookie array unless you don't know what the keys are already. If you don't know the keys you could iterate over the array in a variety of ways:

foreach( $_COOKIE as $key => $value ){
  if( $key == 'TestCookie' ){
    echo $value;

  if( $value == 'Some Random Value' ){
    echo $key;


im just experimenting with different ways to get a php cookie into a variable

This article has been dead for over six months. Start a new discussion instead.