Re: implode/explode vs serialize/unserialize [message #178294 is a reply to message #178292] |
Tue, 29 May 2012 14:19 |
Erwin Moller
Messages: 228 Registered: September 2010
Karma:
|
Senior Member |
|
|
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
|
|
|