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);

print_r($allcookies);
?>

but the page only returns

Array ( [0] => Array )

please help

try

var_dump($_COOKIE);

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

is there any way to do it with explode?

$_COOKIE is already an array.

ok, but i want to split the array

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;
  }

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

  //etc.
}

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

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.