Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6213 > unrolled thread

implode/explode vs serialize/unserialize

Started bybill <william@TechServSys.com>
First post2012-05-29 09:24 -0400
Last post2012-05-29 16:07 +0200
Articles 12 — 5 participants

Back to article view | Back to comp.lang.php


Contents

  implode/explode vs serialize/unserialize bill <william@TechServSys.com> - 2012-05-29 09:24 -0400
    Re: implode/explode vs serialize/unserialize Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-29 09:53 -0400
    Re: implode/explode vs serialize/unserialize Erwin Moller <erwinmollerusenet@xs4all.nl> - 2012-05-29 16:19 +0200
      Re: implode/explode vs serialize/unserialize Erwin Moller <erwinmollerusenet@xs4all.nl> - 2012-05-29 16:22 +0200
      Re: implode/explode vs serialize/unserialize bill <william@TechServSys.com> - 2012-05-30 09:26 -0400
        Re: implode/explode vs serialize/unserialize Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-30 12:57 -0400
          Re: implode/explode vs serialize/unserialize Scott Johnson <noonehome@chalupasworld.com> - 2012-05-30 13:35 -0700
            Re: implode/explode vs serialize/unserialize Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-30 17:37 -0400
              Re: implode/explode vs serialize/unserialize Scott Johnson <noonehome@chalupasworld.com> - 2012-05-31 16:45 -0700
        Re: implode/explode vs serialize/unserialize Erwin Moller <erwinmollerusenet@xs4all.nl> - 2012-05-30 20:37 +0200
        Re: implode/explode vs serialize/unserialize Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-05-30 22:34 +0200
    Re: implode/explode vs serialize/unserialize Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-05-29 16:07 +0200

#6213 — implode/explode vs serialize/unserialize

Frombill <william@TechServSys.com>
Date2012-05-29 09:24 -0400
Subjectimplode/explode vs serialize/unserialize
Message-ID<jq2im6$u4p$1@speranza.aioe.org>
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

[toc] | [next] | [standalone]


#6214

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-05-29 09:53 -0400
Message-ID<jq2kco$opn$1@dont-email.me>
In reply to#6213
On 5/29/2012 9:24 AM, 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

There is no correct answer to your question without a context.  The two 
have entirely different purposes.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#6215

FromErwin Moller <erwinmollerusenet@xs4all.nl>
Date2012-05-29 16:19 +0200
Message-ID<4fc4dae4$0$6908$e4fe514c@news2.news.xs4all.nl>
In reply to#6213
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.

Try the following:

==================================
$a = array( array(1,2,3),array("a","b","c","d"),array("nr1"=>42,"nr2"=>9));
$str_impl = implode($a);
$str_ser = \serialize($a);

echo $str_impl;
echo "<hr>".$str_ser;
==================================

You will see implode makes no sense on (complex) $a.
Implode can only glue arrayelements together.
Serialize can create a string that is suitable to transport/store 
complex structures that can later be unserialized.
You can not store volatile stuff with serialize, like filehandles, 
databaseconnections, etc.

So, in short: implode/explode is NOT serialize/unserialize.

Regards,
Erwin Moller

-- 
"That which can be asserted without evidence, can be dismissed without 
evidence."
-- Christopher Hitchens

[toc] | [prev] | [next] | [standalone]


#6216

FromErwin Moller <erwinmollerusenet@xs4all.nl>
Date2012-05-29 16:22 +0200
Message-ID<4fc4dba8$0$6908$e4fe514c@news2.news.xs4all.nl>
In reply to#6215
On 5/29/2012 4:19 PM, Erwin Moller wrote:

<snip>


> ==================================
> $a = array( array(1,2,3),array("a","b","c","d"),array("nr1"=>42,"nr2"=>9));
> $str_impl = implode($a);
> $str_ser = \serialize($a);

typo correction:
the above should be without the \
thus:
$str_ser = serialize($a);

Regards,
Erwin Moller

-- 
"That which can be asserted without evidence, can be dismissed without 
evidence."
-- Christopher Hitchens

[toc] | [prev] | [next] | [standalone]


#6224

Frombill <william@TechServSys.com>
Date2012-05-30 09:26 -0400
Message-ID<jq576g$phf$1@speranza.aioe.org>
In reply to#6215
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

[toc] | [prev] | [next] | [standalone]


#6227

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-05-30 12:57 -0400
Message-ID<jq5jhu$1k6$1@dont-email.me>
In reply to#6224
On 5/30/2012 9:26 AM, 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

Which would violate rdb normalization rules...

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#6231

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-05-30 13:35 -0700
Message-ID<jq60a8$ot8$1@dont-email.me>
In reply to#6227
On 5/30/2012 9:57 AM, Jerry Stuckle wrote:
> On 5/30/2012 9:26 AM, 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
>
> Which would violate rdb normalization rules...
>

Yes I tried that before while experimenting.

I got lazy and just create a 'user_data' field to store some form data 
without creating an actual field for each one.

Well it worked just fine and danady until I needed to do a search for a 
particular value.

I then had to pull ALL the data, deserialize and search that data.

What a nightmare.  But I did learn some new algorithms in the process.

I suppose sometimes it is our failures that teach us the best.

:)
Scotty

[toc] | [prev] | [next] | [standalone]


#6233

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-05-30 17:37 -0400
Message-ID<jq63uh$ffd$1@dont-email.me>
In reply to#6231
On 5/30/2012 4:35 PM, Scott Johnson wrote:
> On 5/30/2012 9:57 AM, Jerry Stuckle wrote:
<snip>
>> Which would violate rdb normalization rules...
>>
>
> Yes I tried that before while experimenting.
>
> I got lazy and just create a 'user_data' field to store some form data
> without creating an actual field for each one.
>
> Well it worked just fine and danady until I needed to do a search for a
> particular value.
>
> I then had to pull ALL the data, deserialize and search that data.
>
> What a nightmare. But I did learn some new algorithms in the process.
>
> I suppose sometimes it is our failures that teach us the best.
>
> :)
> Scotty

Scotty,

Yup, there are reasons for those rules, and good reasons to follow them! :)

That doesn't mean you should always follow every normalization rule 
every time; there are good reasons why you might not (i.e. performance).

But when you do violate the rules, you should have a good reason and be 
aware of the possible consequences.  After all, having to later change 
the database and rewrite the programs can be a long, involved process!

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#6235

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-05-31 16:45 -0700
Message-ID<jq8vra$cug$1@dont-email.me>
In reply to#6233
On 5/30/2012 2:37 PM, Jerry Stuckle wrote:
> On 5/30/2012 4:35 PM, Scott Johnson wrote:
>> On 5/30/2012 9:57 AM, Jerry Stuckle wrote:
> <snip>
>>> Which would violate rdb normalization rules...
>>>
>>
>> Yes I tried that before while experimenting.
>>
>> I got lazy and just create a 'user_data' field to store some form data
>> without creating an actual field for each one.
>>
>> Well it worked just fine and danady until I needed to do a search for a
>> particular value.
>>
>> I then had to pull ALL the data, deserialize and search that data.
>>
>> What a nightmare. But I did learn some new algorithms in the process.
>>
>> I suppose sometimes it is our failures that teach us the best.
>>
>> :)
>> Scotty
>
> Scotty,
>
> Yup, there are reasons for those rules, and good reasons to follow them! :)
>
> That doesn't mean you should always follow every normalization rule
> every time; there are good reasons why you might not (i.e. performance).
>
> But when you do violate the rules, you should have a good reason and be
> aware of the possible consequences. After all, having to later change
> the database and rewrite the programs can be a long, involved process!
>

No doubt brother.

[toc] | [prev] | [next] | [standalone]


#6229

FromErwin Moller <erwinmollerusenet@xs4all.nl>
Date2012-05-30 20:37 +0200
Message-ID<4fc668f4$0$6932$e4fe514c@news2.news.xs4all.nl>
In reply to#6224
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

[toc] | [prev] | [next] | [standalone]


#6230

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-05-30 22:34 +0200
Message-ID<2323676.ZLtig6B16M@PointedEars.de>
In reply to#6224
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.

Utter nonsense.

> Your shot from the hip is exactly what I was looking for - thanks.

No it is not.  The problem with that approach is that implode() is _not_ 
generally reversible with explode(), even in a simple, one-dimensional array 
where all elements are of the same type.

It does not work if the array elements are of different type or are not 
strings (because the values are type-converted to string, and exploding a 
string results in array of string elements), and it does not work if the 
delimiter (say, `,') is part of any array string element, assuming that all 
values are strings already.  And if you knew exactly what was in an array, 
you would not need to implode() it in the first place.

So if you want to preserve a data structure in string form for later 
restore, use serialize() and unserialize(), nothing else.


PointedEars
-- 
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
  -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

[toc] | [prev] | [next] | [standalone]


#6217

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-05-29 16:07 +0200
Message-ID<6536383.yMDEE7PuCN@PointedEars.de>
In reply to#6213
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.

That is a stupid question, bill #256234 (no pun intended).

In fact, it is not a question at all.
 
> It seems that for 1D arrays implode is better in that is only
> adds 1 character per array element and serialize adds many.

And if any array element contains the delimiter, …
 
> For objects, serialze is the only way to go.

To go where?


PointedEars
-- 
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
  -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web