我从wordpress中名为groups的post-meta字段返回一个序列化数组。
以下是它在postmeta字段中的外观。
a:2:{i:0;s:1:"1";i:1;s:1:"2";}
我如何循环通过这个并运行if语句,即
$mydata = unserialize($meta['groups']);
print_r($mydata);
未序列化的对我不起作用——我从打印中得到的输出低于
a:2:{i:0;s:1:"1";i:1;s:1:"2";}
与上述相同。
任何关于使用以前从未使用过的序列化和非序列化数组的帮助。
magic_quotes
处于活动状态。用stripslashes
:去除其生成的斜线
$mydata = unserialize(stripslashes($meta['groups']));
如果你想从整个GPC数组中去掉斜杠,请使用以下方法(署名请参阅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);
}
print_r(unserialize('a:2:{i:0;s:1:"1";i:1;s:1:"2";}'));
将打印
Array
(
[0] => 1
[1] => 2
)
非序列化操作很好。您如何知道$meta['groups']
是否包含您想要的内容?
以下是我使用命令行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
)
$meta['groups']
似乎不包含序列化的字符串。