Hi, I took this piece of code from

http://www.php.net/manual/en/reserved.variables.php#reserved.variables.cookies

and changed it a little. I want to take the values from the cookie and store them in an array.
This is the code: ($ar is $HTTP_COOKIE_VARS)

function display($ar){
while(list($key,$val)=each($ar)){

 $arr[$key] = $val;
 echo "$key = $arr[$key]<BR>";
     if (is_array($val)){
      display($val);

    }

}

   $v[$r]=$arr["Name"];
   $r++;
   $v[$r]=$arr["BuyAddress1"];
   $r++;
   $v[$r]=$arr["BuyAddress2"];
   $r++;
   $v[$r]=$arr["BuyCity"];
   $r++;
   $v[$r]=$arr["BuyState"];
   $r++;
   $v[$r]=$arr["BuyZip"];
   $r++;
   $v[$r]=$arr["BuyEmail"];
   $r++;
   $v[$r]=$arr["DayPhone"];
   $r++;
   $v[$r]=$arr["EvePhone"];
   $r++;

}

Now, the array ($v) would be empty. So, I tested the process.
There are two loops. The first time everyting works fine. These are the results I get:
cookie = Array
Name = Panos A. (NY)
BuyAddress1 = 9919 Ave Z
BuyAddress2 = ap 2B
BuyCity = Brooklyn
BuyState = NY
BuyZip = 11235
BuyEmail = appanos@hotmail.com
DayPhone = (718) 555 - 3688
EvePhone = 718 555-5107
The second times it erases all the array values so they come out empty:
cookie =
Name =
BuyAddress1 =
BuyAddress2 =
BuyCity =
BuyState =
BuyZip =
BuyEmail =
DayPhone =
EvePhone =
I tried setting a count ($c=1; code here and then $c++) so that if ($c != 1) it won't perform the loop. Doesn't work and it doesn't make sence to me why it does this.
Help please, goin' slightly crazy here.
Thank u,
Panos A.

    Ahhh, I think I see. Are you trying to do something like this: Call the function display ($_COOKIE); and then do something with the array $v? Well the array $v isn't global, so as soon as the function ends, you can't access it. What you should do is at the end of your function, put:

    return $v;

    and back where you make a call to the function, instead of just putting display ($COOKIE);
    put $v = display ($
    COOKIE);

    That way, $v is now the array that was defined inside your function. Is that what you want?

      Also, if you are using $HTTP_COOKIE_VARS (and not $COOKIE, as Rohan suggests), you will nned to declare $HTTP_COOKIE_VARS inside the function as global as well. But use $COOKIE if you can (PHP>=4.1); less typing 😉

        Thanks guys for ur input.
        I finally figured it out. This is what I wanted to do:

        function getTheInfo($cookiename,$count) {
        global $v;
        if (isset ($cookiename)) {
                 while (list ($name, $value) = each ($cookiename)) {
                 $v[$count] = $value;
                 $count++;
                         }
                }
        }
        getTheInfo($HTTP_COOKIE_VARS["aad"],0);
        

        So now I have an array ($v) with all the cookie's values.
        So simple!
        I guess that's what u get for being a newbie 🙂
        Thanks again
        Panos A.

          Write a Reply...