-3

Below are my database value and i want to retrieve answer_id using php.

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:\"\";}}
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
vishal
  • 27
  • 8
  • 2
    See:[What kind of string is this?](http://stackoverflow.com/q/28614078) and [How can I access an object/array?](http://stackoverflow.com/q/30680938) – Rizier123 May 14 '16 at 12:06
  • 2
    Take a moment to read through the [editing help](http://stackoverflow.com/editing-help) in the help center. Formatting on Stack Overflow is different than other sites. The better your post looks, the easier it is for others to read and understand it. – Rizier123 May 14 '16 at 12:06

2 Answers2

1

Try below code, it will return answer id to you.

$str = "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:\"\";}}";
$arry = unserialize($str);

echo $arry[0]->answer_id;
Gokul Shinde
  • 957
  • 3
  • 10
  • 30
0

The Issues is that your serialized String contains back-slashes which would mess with the serialized object. Solution: Remove the backslashes and unserialzed the string and you'd get your object back:

    <?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)

You can test it here: https://eval.in/571535

And now; to get you answer_id You can simply do this:

    <?php


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

        var_dump($answerID);  // DUMPS: '2'
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • @vishal Anytime Mate... **;-)** You may check this as the answer so that someone with similar issues can see what worked for you though... – Poiz May 14 '16 at 12:21