1

I'm going to use a plug-in in WordPress called RegistrationMagic. And I need to access the data saved for further use in front-end for a reason.

I was looking the tables in MySQL and found the data of the users like that:

a: 5: {
  i: 6;O: 8: "stdClass": 3: {
    s: 5: "label";s: 10: "First Name";s: 5: "value";s: 6: "Danilo";s: 4: "type";s: 5: "Fname";
  }i: 7;O: 8: "stdClass": 3: {
    s: 5: "label";s: 9: "Last Name";s: 5: "value";s: 5: "Teste";s: 4: "type";s: 5: "Lname";
  }i: 5;O: 8: "stdClass": 3: {
    s: 5: "label";s: 5: "Email";s: 5: "value";s: 26: "some@email.com";s: 4: "type";s: 5: "Email";
  }i: 8;O: 8: "stdClass": 3: {
    s: 5: "label";s: 7: "Website";s: 5: "value";s: 17: "http://teste.com/";s: 4: "type";s: 7: "Website";
  }i: 9;O: 8: "stdClass": 3: {
    s: 5: "label";s: 43: "Do you agree with our terms and conditions?";s: 5: "value";s: 2: "on";s: 4: "type";s: 5: "Terms";
  }
}

Someone can help me to understand what is that? It is JSON? Is possible work with it, like catch in a loop or something?

Sorry if this is a repost, because I always see this in my WordPress databases, but never figured out what is.

1 Answer 1

2

It's a serialized string. Here's a good description of what it is:

A PHP array or object or other complex data structure cannot be transported or stored or otherwise used outside of a running PHP script. If you want to persist such a complex data structure beyond a single run of a script, you need to serialize it. That just means to put the structure into a "lower common denominator" that can be handled by things other than PHP, like databases, text files, sockets. The standard PHP function serialize is just a format to express such a thing, it serializes a data structure into a string representation that's unique to PHP and can be reversed into a PHP object using unserialize. There are many other formats though, like JSON or XML.

So if you have a PHP array, object, etc., say

$array = array(
    'key value' => array(
        'more info' => array(1,2,3)
    ),
    'another key' => array(
        'and so on' => '<span>so on</span>'
    )
);  

With PHP you can use serialize() and unserialize() to turn it into a string. You can see yourself with a script like:

$storeArray = serialize($array);

echo "<pre>".print_r($storeArray,true)."</pre>";

$restore = unserialize($storeArray);

echo "<pre>".print_r($restore,true)."</pre>";

UPDATE: and as @TomJNowell reminded (thanks!):

WP auto-serialises and deserialises on the fly if you pass objects or arrays into APIs for saving

With Wordpress, for example with the Options API, passing your array into add_option() will save your array as a serialized string in the _options table. Retrieving the value with get_option() will auto-unseralizes it for you, and the value got will be the same as the original $array you passed added in:

add_option('my_array', $array);

$my_array = get_option('my_array');

echo "<pre>".print_r($my_array,true)."<pre>";
2
  • 2
    you should have noted that WP auto-serialises and deserialises on the fly if you pass objects or arrays into APIs for saving
    – Tom J Nowell
    Dec 20, 2017 at 20:40
  • Thank you sir! I'll run some tests and if goes ok I check as solved. Very clear and direct to the point. Dec 21, 2017 at 14:05

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.