Reputation: 1131
I am return a serialize array from a post meta field in wordpress called groups.
here is how it looks in to post meta field.
a:2:{i:0;s:1:"1";i:1;s:1:"2";}
how can i loop trough this and run an if statement i.e.
$mydata = unserialize($meta['groups']);
print_r($mydata);
The unserialzed isnt working for me the ouput i get from the print_r is below
a:2:{i:0;s:1:"1";i:1;s:1:"2";}
which is same as above.
Any help on working with serialized and unserialzed arrays never used before.
Upvotes: 0
Views: 17749
Reputation: 20492
Here is what I obtained using command-line PHP:
php > $x = unserialize('a:2:{i:0;s:1:"1";i:1;s:1:"2";}');
php > print_r($x);
Array
(
[0] => 1
[1] => 2
)
It seems that $meta['groups']
does not contain the serialized string.
Upvotes: 0
Reputation: 19466
print_r(unserialize('a:2:{i:0;s:1:"1";i:1;s:1:"2";}'));
will print
Array
(
[0] => 1
[1] => 2
)
The unserialization works just fine. How do you know if $meta['groups']
contains what you want?
Upvotes: 0
Reputation: 32691
Propably magic_quotes
is active. Strip the slashes generated by it with stripslashes
:
$mydata = unserialize(stripslashes($meta['groups']));
If you want to strip slashes from the whole GPC-Array use this (Credits go to this comment on PHP.net):
if (get_magic_quotes_gpc()) {
$strip_slashes_deep = function ($value) use (&$strip_slashes_deep) {
return is_array($value) ? array_map($strip_slashes_deep, $value) : stripslashes($value);
};
$_GET = array_map($strip_slashes_deep, $_GET);
$_POST = array_map($strip_slashes_deep, $_POST);
$_COOKIE = array_map($strip_slashes_deep, $_COOKIE);
}
Upvotes: 1