Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with serialized data in Wordpress

I have the following string of serialized data in a Wordpress custom field:

$first_string = 'a:9:{s:5:"email";s:13:"[email protected]";s:4:"name";s:15:"Werner  
Etsebeth";s:8:"address1";s:17:"1 Giligans  
Island";s:8:"address2";s:1:"5";s:4:"city";s:9:"Cape  
Town";s:5:"state";s:2:"AL";s:3:"zip";s:4:"7460";s:7:"
country";s:2:"US";s:5:"phone";s:0:"";}

$second_string = 'a:1:{i:4;a:1:{i:0;a:6: 
{s:3:"SKU";s:0:"";s:4:"name";s:12:"Hypnotherapy";s:3:"url";s:72:"http://localhost
/mindworksa.co.za/wordpress/store/products/hypnotherapy
/";s:5:"price";s:5:"50.00";s:8:"quantity";s:1:"1";s:8:"download";s:0:"";}}}'

How do I assign the info to variables so I can access individually eg $SKU = "", $name = etc.

I've never worked with serialized data before and any help would be greatly appreciated.

Many thanks

like image 546
iball Avatar asked Nov 10 '12 05:11

iball


1 Answers

I am unable to unserialize your example (did you paste it in correctly?) but Wordpress uses serialize() to serialize objects to store them in the database.

You can unserialize them using unserialize().

A quick example:

$serialized = 'a:3:{i:0;s:5:"apple";i:1;s:6:"banana";i:2;s:6:"orange";}';
var_dump(unserialize($serialized));

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
like image 51
Aamir Avatar answered Sep 27 '22 19:09

Aamir