LimeSurvey使用来自数据库的JSON字符串

时间:2014-10-29 16:31:43

标签: mysql json pdo

当我使用LimeSurvey的管理工具向我的调查添加其他字段时,数据库中的attributedescriptions字段如下所示;

a:1:
{   
    s:11:"attribute_1";
    a:4:
    {
        s:11:"description";
        s:4:"Unit";
        s:9:"mandatory";
        s:1:"N";
        s:13:"show_register";
        s:1:"N";
        s:7:"cpdbmap";
        s:0:"";
    }
}

当我输入另一个时,数据库中的字段看起来像这样;

a:2:
{
    s:11:"attribute_1";
    a:4:
    {
        s:11:"description";
        s:4:"Unit";
        s:9:"mandatory";
        s:1:"N";
        s:13:"show_register";
        s:1:"N";
        s:7:"cpdbmap";
        s:0:"";
    }

    s:11:"attribute_2";
    a:4:
    {
        s:11:"description";
        s:9:"Something";
        s:9:"mandatory";
        s:1:"N";
        s:13:"show_register";
        s:1:"N";
        s:7:"cpdbmap";
        s:0:"";
    }
}

我现在需要一种动态的方法来将单词“Unit”和“Something”输入到我可以使用的数组中。

这是获取json字符串的代码;

$sql = $dbh->prepare($sql);
$sql->execute();
$result = $sql->fetchAll(PDO::FETCH_ASSOC);

print $result[0]["attributedescriptions"];

我在PDO很糟糕。我试过了;

$result = var_dump(json_decode($result[0]["attributedescriptions"], true);

$result = var_dump(json_decode($result["attributedescriptions"], true);

$result = var_dump(json_decode($result[0], true);

我收到了错误;

Warning: json_decode() expects parameter 1 to be string, array given in /var/www/html/surveys/survey-admin/functions/functions.php on line 189 NULL

1 个答案:

答案 0 :(得分:1)

这是 JSON,它是序列化数据:

<强>序列化

$serialized_data = base64_encode(serialize($data));

<强>解序列化

$unserialized_data = unserialize(base64_decode($serialized_data));

base64_encode()用于避免损坏,如果数据已损坏,unserialize()将返回false。