在php中检索答案id
下面是我的数据库值,我想用php检索答案id在php中检索答案id,php,Php,下面是我的数据库值,我想用php检索答案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\
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:\"\";}}
尝试下面的代码,它将返回答案id给你
$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;
问题是序列化字符串包含反斜杠,这会弄乱序列化对象。解决方案:删除反斜杠并取消字符串序列化,您将得到您的对象:
<?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)
请参阅:然后花点时间阅读帮助中心中的。堆栈溢出上的格式设置与其他站点不同。你的帖子越好看,别人越容易阅读和理解。@vishal Anytime Mate…;-)您可以将此作为答案进行检查,以便有类似问题的人可以看到什么对您有效。。。
<?php
$objData = $objUnSerialized[0];
$answerID = $objData->answer_id;
var_dump($answerID); // DUMPS: '2'