9

So I have been wondering for a while now what these entries are and what they do, also what the format of them are. They look similar to JSON but are definitely not JSON.

a:1:{s:12:"jetlocations";a:2:{s:5:"label";s:12:"JetLocations";s:4:"type";s:6:"editor";}}

I have searched to try and find out what these might be but come up with nothing.

1 Answer 1

11

That's a Serialized Array which turns an array into a simple string so that it can be inserted into a database ( since you cannot just insert arrays into a database as they are ).

For more information you can read into the PHP docs for serialize()

There is also some WordPress functions which do the same: maybe_serialize() and maybe_unserialize().

This usually happens ( automatically ) whenever you pass an array to the update_post_meta() function. In order to get the array back, you can use get_post_meta() and pass in a true value to $single.

$single

(boolean) (optional) If set to true then the function will return a single result, as a string. If false, or not set, then the function returns an array of the custom fields. This may not be intuitive in the context of serialized arrays. If you fetch a serialized array with this method you want $single to be true to actually get an unserialized array back. If you pass in false, or leave it out, you will have an array of one, and the value at index 0 will be the serialized string.

Default: false

get_option() will also return an unserialized array ready for use by default.


If we break down the string it looks like this:

a:1:{s:12:"jetlocations";a:2:{s:5:"label";s:12:"JetLocations";s:4:"type";s:6:"editor";}}

a:1:{} = Specifies that there is an array with a count of 1

s:12: = The only index in the array is a string of 12 characters long - "jetlocations"

a:2: = jetlocations then holds an array with the count of 2

s:5: = The first index is a string of 5 characters long - "label"

s:12: = The value of "label" is also a string of 12 characers long - "JetLocations"

s:4: = Our second index is a string of 4 characters logn - "type"

s:6: = The value of "type" is a string of 6 characters long - "editor"

So if we put this all together we get an array that looks like this:

array(
    'jetlocations' => array(
        'label' => 'JetLocations',
        'type'  => 'editor'
    )
);
0

Your Answer

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

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