How to get all values from explode?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eye_nid_help
Forum Newbie
Posts: 5
Joined: Sun Nov 14, 2010 1:56 am

How to get all values from explode?

Post by eye_nid_help »

Hi all,I am really really new to PHP and don't know a lot so i was wondering if any of you all could help me:

I am creating a website where one can post things and others can comment and like them. But the problem is that i want to store the id of the post into a cookie so that the person can only like it 1 time. I am storing multiple ids into an array and then into a cookie.

When i want to retrieve it, i am using the explode() function. How can i get all the values inside the array? Because it is quite tedious to do this:

Code: Select all

$cookie_liked[0];
$cookie_liked[1];
$cookie_liked[2];
$cookie_liked[3];
Is there any syntax that can include all?

Any help is appreciated. Thnx!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to get all values from explode?

Post by Celauran »

Code: Select all

for ($i = 0; $i < count($cookie_liked); $i++)
{
    echo $cookie_liked[$i];
}
I just used echo for the sake of example.
eye_nid_help
Forum Newbie
Posts: 5
Joined: Sun Nov 14, 2010 1:56 am

Re: How to get all values from explode?

Post by eye_nid_help »

Thanks for your response, although there is still 1 thing i'm not sure of. Can i store all those value in 1 variable? or do i have to store each 1 in a seperate variable?

Sorry to bother you.
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

Re: How to get all values from explode?

Post by dimxasnewfrozen »

So you're exploding the cookie array and want to store the values in a single variable?

Code: Select all

// Depending on the configuration
$my_new_value = "";

foreach($cookie_liked as $cookie_value){
	$my_new_value .=  $cookie_value . "-";
}
So $my_new_value would just be a long string of values which you will need to explode again to do anything with. I'm not sure what the purpose is.
eye_nid_help
Forum Newbie
Posts: 5
Joined: Sun Nov 14, 2010 1:56 am

Re: How to get all values from explode?

Post by eye_nid_help »

So each of the values are seperated by "-"?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: How to get all values from explode?

Post by Jonah Bron »

eye_nid_help wrote:Can i store all those value in 1 variable?
There are. They're all in $cookie_liked.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to get all values from explode?

Post by McInfo »

Cookie data needs to be in string format.
PHP wrote:setcookie() expects parameter 2 to be string
A quick way to make arrays portable as strings is with the serialize() function and its companion unserialize().

Be aware that cookie data is url-encoded, so all the punctuation symbols used for serialization will be expanded to three times their decoded size. Cookies are sent back to the server on every request, so the size of the data should be a consideration.
eye_nid_help
Forum Newbie
Posts: 5
Joined: Sun Nov 14, 2010 1:56 am

Re: How to get all values from explode?

Post by eye_nid_help »

McInfo wrote:Cookie data needs to be in string format.
A quick way to make arrays portable as strings is with the serialize() function and its companion unserialize().
I used the implode() and explode() function,is there any difference with that?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to get all values from explode?

Post by McInfo »

Imploding an array will typically result in a smaller serialized string than using serialize(). Using a "glue" that doesn't expand when url-encoded (like hyphen (-), underscore (_), or space) will result in even smaller cookie data.

Some benefits of serialize() are convenience, standardization, and the ability to handle objects.
Post Reply