views:

530

answers:

4

a:3:{i:0;i:4;i:1;i:3;i:2;i:2;}

Am I right to say that this is an array of size 3 where the key value pairs are 0->4, 1->3, and 2->2? If so, I find this representation awfully confusing. At first, I thought it was a listing of values (or the array contained {0, 4, 1, 3, 2, 2}), but I figured that the a:3: was the size of the array. And if 3 was the size, then both the keys AND values appeared in the brackets with no way of clearly identifying a key/value pair without counting off.

To clarify where I'm coming from...

Why did the PHP developers choose to serialize in this manner? What advantage does this have over, let's say the way var_dump and/or var_export displays its data?

A: 

Why don't you use the unserialize() function to restore the data to how it was before?

JamShady
But I'm trying to understand what serialize() is giving me.
Thomas Owens
Shame your question didn't make that clear before you edited it and downvoted my comment. You first seem to ask what the data is, and then you're asking why PHP developers did it that way - two different questions with different answers.
JamShady
I removed the downvote. I thought I included that part, but I must have posted before I was done. Sorry about that.
Thomas Owens
+4  A: 

Yes that's array(4, 3, 2)

a for array, i for integer as key then value. You would have to count to get to a specific one, but PHP always deserialises the whole lot, so it has a count anyway.

Edit: It's not too confusing when you get used to it, but it can be somewhat long-winded compared to, e.g. JSON

Note: var_export() does not handle circular references as it would be close to impossible to generate parsable PHP code for that. If you want to do something with the full representation of an array or object, use serialize().

Greg
+1  A: 
$string="a:3:{i:0;i:4;i:1;i:3;i:2;i:2;}";
$array=unserialize($string);
print_r($array);

outpts:

Array
(
    [0] => 4
    [1] => 3
    [2] => 2
)

If think the point is that PHP does not differentiate between integer indexed arrays and string indexed hashtables. The serialization format can be used for hashtables exactly the same way: a:<<size>>:{<<keytype>>:<<key>>;<<valuetype>>:<<value>>;...}

As the format is not intended to be human readable but rather to provide a common format to represent all PHP variable types (with exception of resources), I think it's more simple to use the given format because the underlying variable can be reconstructed by reading the string character by character.

Stefan Gehrig
I'm trying to determine the advantages of this format serialization versus others. In terms of data storage, it works, but it's also more difficult for human reading. Are there performance differences? Is it just a preference?
Thomas Owens
Edited my post to try to answer your questions...
Stefan Gehrig
+1  A: 

Serialized PHP data is not really intended to be human readable - that is not a goal of the format as far as I know.

I think the biggest reason the format looks the way it does is for brevity, and its form may also have underpinnings tied to the speed at which it can be processed.

Peter Bailey