For example, when I can set cookie array like this

for ($index=0; $index<5; $index++)
{
setcookie("mycookiearray[$index]", $index);
}

Next page when I call the cookie

for ($index=0; $index<count($mycookiearray); $index++)
{
echo($mycookiearray[$index]);
echo("<br>");
}

It will shows me

1
2
3
4

Now I want to only delete the last element of the cookie array $mycookiearray (not just set the value to null but actually delete that element physically.) and then when I call the

count($mycookiearray) it should return 4 instead of 5.

How can I do that?

Thanks!

    I used serialized on the array only when I tried to store the array value in a database table field.

    I can see this approach will work to solve the problem. But that also means I need to unserialize to get the cookie array every time.

    which one is better, use cookie array or use the one cookie + serialize?

    And anothoer question

    Both cookie array will be considered as one cookie right in the terms of the 20 cookies per domain limitation?

    Thanks.

      serialize is the best you can store even 100 elemets in one single cookie and you can't set 100 cookies :rolleyes:

      and again try always to think how to set the less amount of cookies is insane set a lot of cookies.

      if you set an array using serialize in one cookie you can set in your 20 cookies 20 different arrays

        But array is consider as one cookie, am I right?

          I didn't get your question but

          array is an array and cookie is cookie.

          You can have 100 arrays in your script

          and you can only have 20 cookies in a domain

          $HTTP_COOKIE_VARS is an array of cookies

          😕

            For example, when I can set cookie array like this

            for ($index=0; $index<20; $index++)
            {
            setcookie("mycookiearray[$index]", $index);
            }

            When I set cookies like this. It will be consider as One cookie used. Not twenty cookies used. Right?

            In the professional php programming book it suggest that to use the array to save the cookie value to fit in the 20 cookies per domain requirement.

            Thanks for you help.

              as far as $_COOKIES go, an array of cookies is ONE variable, however the browser might not see it this way... one way to find out... (besides searching which most people wont do anyways)

              TRY IT!!!!!!!!!! YAY LETS GO! SOMEONE TRY IT! nudges searain (its your post you get the torch to go and try it 😉)

                I tested it.

                for ($index=0; $index<40; $index++)
                {
                setcookie("mycookiearray[$index]", $index);
                }

                It will be considered by the browser as 40 cookies. So only the last 20 will be saved in the cookie values.

                  Originally posted by searain
                  I tested it.

                  for ($index=0; $index<40; $index++)
                  {
                  setcookie("mycookiearray[$index]", $index);
                  }

                  It will be considered by the browser as 40 cookies. So only the last 20 will be saved in the cookie values.

                  awesome, helpful, archivable, information 🙂

                  guess now you get to learn how to use (un)serialize() then 😉

                    Write a Reply...