1

I created a custom key and wanted to store multiple values in the key with an array in my plugin using a drop down box, however, I printed out the array and came back out with this value...

Array ( [0] => a:2:{i:0;s:2:"75";i:1;s:2:"68";} )

My thinking was that it would just add another value [1] => 'content', [2] => 'lkajsdf'. That is how I wanted it to happen, but instead I get the result above. It is storing the two ID values that I selected 75 and 68.

So, what does that result mean?

1

3 Answers 3

5

You're looking at a serialized representation of the array Array( '75', '68' ). Serialization is the process by which PHP stores a data object as a string, much like the manner in which JSON is a string representation of a Javascript object. PHP data structures may be converted into a serialized format via PHP's serialize(), and back again using unserialize().

WordPress also provides functions which perform the necessary action only when needed in order to prevent accidental double-serialization or unserialization: maybe_serialize() and maybe_unserialize(). You can also check yourself with WordPress' is_serialized().

From the comments on PHP's serialize(), the anatomy of a serialized data object is as follows:

String s:size:value;

Integer i:value;

Boolean b:value; (does not store "true" or "false", does store 1 or 0)

Null N;

Array a:size:{key definition;value definition;(repeated per element)}

Object O:strlen(object name):object name:object

size s:strlen(property name):property name:property definition;(repeated per property)

String values are always in double quotes.

Array keys are always integers or strings; using other types as keys produces undesirable results:

  • null => 'value' equates to 's:0:"";s:5:"value";'
  • true => 'value' equates to 'i:1;s:5:"value";'
  • false => 'value' equates to 'i:0;s:5:"value";'
  • array(whatever the contents) => 'value' equates to an "illegal offset type" warning because you can't use an array as a key; however, if you use a variable containing an array as a key, it will equate to 's:5:"Array";s:5:"value";', and attempting to use an object as a key will result in the same behavior as using an array will.

Therefore, we can interpret your particular serialized array a:2:{i:0;s:2:"75";i:1;s:2:"68";} as such:

  • a:2:{ an array of length 2, containing:
    • i:0; at the integer key 0 (i.e. index 0):
      • s:2:"75"; a string of length 2 with the value "75"
    • i:1; at the integer key 1 (i.e. index 1):
      • s:2:"68" a string of length 2 with the value "68"
  • } end of the array

To alternately reflect the values of your items 'content' and 'lkajsdf' rather than their numerical identifiers, then, the array Array( 'content', 'lkajsdf' ) would be serialized as

a:2:{i:0;s:7:"content";i:1;s:7:"lkajsdf";}

Knowing this can come in handy if you ever need to alter some of WordPress's settings, or selectively activate/deactivate specific plugins directly from the database.

2
  • We've all run into the question at one point or another ;) . I think perhaps the most important thing to keep in mind with WordPress and serialization is that WordPress stores some data in the database as a serialized string. If you ever go to perform a search-and-replace operation on your WordPress database, think twice, because it may well invalidate any serialized data containing that string. There are a few serialization-aware database search-and-replace tools for that reason.
    – bosco
    May 5, 2015 at 18:25
  • 2
    +1 good overview and I like how you break up the serialized string with comments, similar to how regexp patterns are often explained.
    – birgire
    May 5, 2015 at 20:07
0

It seems like the your multiple values were simply serialized by PHP's serialize() function.

You can unserialize it with unserialize().

0

After unserializing the result is:

Array
(
   [0] => 75
   [1] => 68
)

You can find the format of PHP serialized array here: Serialized PHP string

a:2 is object array here 's':<i> is the string where i is the length of string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.