在PHP中检索answer_id

可见的

以下是我的数据库值,我想使用php检索answer_id。

a:1:{i:0;O:8:\"stdClass\":7:{s:11:\"question_id\";s:1:\"1\";s:13:\"question_text\";s:18:\"This is question 1\";s:9:\"answer_id\";s:1:\"2\";s:11:\"answer_text\";s:4:\"asss\";s:11:\"points_base\";s:1:\"2\";s:6:\"points\";s:1:\"2\";s:15:\"custom_response\";s:0:\"\";}}
Poiz

问题是您的序列化字符串包含反斜杠,该反斜杠会干扰序列化的对象。解决方案:删除反斜杠并反序列化字符串,然后将对象找回:

    <?php


        $strSerializedWithSlashes       = 'a:1:{i:0;O:8:\"stdClass\":7:{s:11:\"question_id\";s:1:\"1\";s:13:\"question_text\";s:18:\"This is question 1\";s:9:\"answer_id\";s:1:\"2\";s:11:\"answer_text\";s:4:\"asss\";s:11:\"points_base\";s:1:\"2\";s:6:\"points\";s:1:\"2\";s:15:\"custom_response\";s:0:\"\";}}';
        $strSerializedWithoutSlashes    = str_replace("\\", "", $strSerializedWithSlashes);
        $objUnSerialized                = unserialize($strSerializedWithoutSlashes);

        var_dump($objUnSerialized);


        // DUMPS::
        array (size=1)
          0 => 
            object(stdClass)[1]
              public 'question_id' => string '1' (length=1)
              public 'question_text' => string 'This is question 1' (length=18)
              public 'answer_id' => string '2' (length=1)
              public 'answer_text' => string 'asss' (length=4)
              public 'points_base' => string '2' (length=1)
              public 'points' => string '2' (length=1)
              public 'custom_response' => string '' (length=0)

您可以在这里进行测试:https//eval.in/571535

现在; 让您获得answer_id您可以简单地执行以下操作:

    <?php


        $objData    = $objUnSerialized[0];
        $answerID   = $objData->answer_id;

        var_dump($answerID);  // DUMPS: '2'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章