Page 1 of 1
How to get all values from explode?
Posted: Wed Nov 24, 2010 3:40 am
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!
Re: How to get all values from explode?
Posted: Wed Nov 24, 2010 6:46 am
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.
Re: How to get all values from explode?
Posted: Wed Nov 24, 2010 8:12 am
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.
Re: How to get all values from explode?
Posted: Wed Nov 24, 2010 3:08 pm
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.
Re: How to get all values from explode?
Posted: Wed Nov 24, 2010 7:31 pm
by eye_nid_help
So each of the values are seperated by "-"?
Re: How to get all values from explode?
Posted: Wed Nov 24, 2010 10:36 pm
by Jonah Bron
eye_nid_help wrote:Can i store all those value in 1 variable?
There are. They're all in $cookie_liked.
Re: How to get all values from explode?
Posted: Thu Nov 25, 2010 12:46 am
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.
Re: How to get all values from explode?
Posted: Thu Nov 25, 2010 6:16 am
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?
Re: How to get all values from explode?
Posted: Thu Nov 25, 2010 9:23 am
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.