Php 将数组到字符串转换为foreach

Php 将数组到字符串转换为foreach,php,Php,当var_dump为“$valReponse”返回良好的值时,我不理解为什么会出现此错误 我使用foreach是因为“$reponses”是一个字符串数组。我试图将数据插入表中,结果出现错误“Array conversion to sting”。我哪里出错了 PHP: foreach ($reponses as $reponse => $valReponse) { var_dump($valReponse); if ($insert_reponses = $this->g

当var_dump为“$valReponse”返回良好的值时,我不理解为什么会出现此错误

我使用foreach是因为“$reponses”是一个字符串数组。我试图将数据插入表中,结果出现错误“Array conversion to sting”。我哪里出错了

PHP:

foreach ($reponses as $reponse => $valReponse) {

  var_dump($valReponse);

  if ($insert_reponses = $this->getConnexion()->prepare('INSERT INTO SD_reponses (reponse_val, question_id) VALUES (?, ?)') or die(mysqli_error($this->getConnexion()))) {

    $insert_reponses->bind_param("si", $valReponse, $question_id);
    $insert_reponses->execute();
    $insert_reponses-> close();

  } else {
    return false;
  }
}

return true;
var_转储结果:

array (size=1)  0 => string 'Answer 1 ' (length=9)<br>
array (size=1)  0 => string 'Answer 2 ' (length=9)
array(size=1)0=>string“Answer 1”(长度=9)
数组(大小=1)0=>字符串“答案2”(长度=9)

但是,在数据库的
response\u val
列中,插入值“Array”而不是我期望的值。

您的
$valResponse
变量是一个包含一个元素的数组,而不是字符串。您需要这样做:

$insert_reponses->bind_param("si", $valReponse[0], $question_id);

$valResponse
变量是包含一个元素的数组,而不是字符串。您需要这样做:

$insert_reponses->bind_param("si", $valReponse[0], $question_id);

如果不确定第一个数组项的索引(/关联),请使用:

  $insert_reponses->bind_param("si", current(array_values($valReponse)), $question_id);


如果不确定第一个数组项的索引(/关联),请使用:

  $insert_reponses->bind_param("si", current(array_values($valReponse)), $question_id);


由于一次不幸的操纵,我今天早上永久删除了我的课程和它吸引人的文件。所以我必须重写我的PHP文件,现在如果我把“$valReponse[0]”放在xdebug中,xdebug对我说,“只有变量应该通过引用传递”。然而,如果我离开“$Valresponse”,它会起作用。一个解释?由于一个不幸的操纵,我今天早上永久删除了我的课程和它吸引人的文件。所以我必须重写我的PHP文件,现在如果我把“$valReponse[0]”放在xdebug中,xdebug对我说,“只有变量应该通过引用传递”。然而,如果我离开“$Valresponse”,它会起作用。解释?