FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » implode/explode vs serialize/unserialize
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: implode/explode vs serialize/unserialize [message #178308 is a reply to message #178303] Wed, 30 May 2012 18:37 Go to previous messageGo to previous message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma:
Senior Member
On 5/30/2012 3:26 PM, bill wrote:
> On 5/29/2012 10:19 AM, Erwin Moller wrote:
>> On 5/29/2012 3:24 PM, bill wrote:
>>> Would one of the more knowledgeable folk discuss when it is
>>> appropriate
>>> to use implode/explode and when it is appropriate to use
>>> serialize/unserialize.
>>>
>>> It seems that for 1D arrays implode is better in that is only
>>> adds 1
>>> character per array element and serialize adds many.
>>>
>>> For objects, serialze is the only way to go.
>>>
>>> in between ?
>>>
>>> bill
>>
>> That is a vague question without more context.
>> Shooting from the hip I would suggest you use serialize only if
>> you need to store/transport a complex structure.
>> And use implode and explode only when you want to create a string
>> from a simple array (no arrays in arrays), and the other way around.
>>
>>
>
> Erwin,
>
> Your shot from the hip is exactly what I was looking for - thanks.
>
> As a learner of PHP, the context is that both are used to convert arrays
> to a form that can be stored in a mysql database.
>
> bill


If you want to store the content of arrays in your database, I suggest
you have another look at your current approach. I think you are heading
the wrong direction.

You haven't stated exactly what you are trying to achieve, but I think
you don't want to implode an array and store that as a string.
While it is possible, it will be harder for you to later retrieve the
info, or join it to another table, or group it, etc.
Actually all regular database actions I can think of would benefit from
the normal approach, which I describe below:

Imagine a table that stores answers that people gave you for your online
poll.
You ask 3 questions:
q1) Rate my site's layout (1=poor, 2,3,4, 5=great)
q2) Rate my site's content (1=poor, 2,3,4, 5=great)
q3) Rate my site's structure (1=poor, 2,3,4, 5=great)

BAD STORAGE:
You can create a table like this:
CREATE TABLE tblPollAnswers(
visitorid integer PRIMARY KEY,
impl_answers TEXT
)

The visitorid is just some imaginary unique number. It isn't important
for the example.
Now, if you implode the answers to the questions, you end up with
content that might look like this:

SELECT visitorid, impl_answers from tblPollAnswers;

visitorid impl_answers
22 "2,4,1"
24 "1,2,3"
44 "5,3,3"
443 "1,1,1"
598 "1,4,3"
etc.
Now try to figure out what the average rating for q1, q2 or q3 is.
You'll have to rip apart the string in impl_answers, make an integer of
it again, sum them, etc.
It is messy and errorprone.

GOOD STORAGE:
CREATE TABLE tblPollAnswers(
visitorid integer PRIMARY KEY,
questionid integer,
answer integer
)

SELECT visitorid, questionid, answer from tblPollAnswers;

visitorid questionid answer
22 1 2
22 2 4
22 3 1
24 1 1
24 2 2
24 2 3
44 1 5
44 2 3
44 3 3
etc.

Now you can easily access, group, etc all data.
eg:
Select questionid, AVG(answer) FROM tblPollAnswers GROUP BY questionid;

While the second approach might look more complicated on first
inspection, it is much much easier in the long run.
I would also advise to get a good book on the basics of databases and
how to design them. The basics are not rocket-science at all.

I hope this helped.

Good luck

Regards,
Erwin Moller

--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: CFP - DEIS2012 - Czech Republic - SDIWC
Next Topic: PHP Concatenate
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 22 11:01:31 GMT 2024

Total time taken to generate the page: 0.05132 seconds