【问题标题】:Array to string conversion - Associative Array数组到字符串的转换 - 关联数组
【发布时间】:2015-02-22 01:49:16
【问题描述】:

我使用下面的代码来解析一个问题及其选择。

$entries = preg_split('/(?=[a-z\d]+\.(?!\d))/', $str, -1, PREG_SPLIT_NO_EMPTY);
$arrAnswers = array();
$arrQuestions = array();
$id = 0; //Assuming your table is empty

foreach($entries as $entry) { //Loop through the grabbed records
  if(is_numeric(substr($entry, 0, 1)) === true) { //Is it a question?
     $id++;     
     $arrAnswers[$id] = array();
     $arrQuestions[$id] = '(\''. $entry .'\')'; 
  } else { //Ok, it's a possible answer to a question
     $arrAnswers[$id][] = '(\''. $entry .'\', '. $id .', 0)';
  }
}
      echo "<pre>";
        print_r($arrQuestions);
      echo "<pre>";

      echo "<br>";

      echo "<pre>";
        print_r($arrAnswers);
      echo "<pre>";

返回这些数组结果:

$arrQuestions output

Array
    (
        [1] => ('1. What is foo?')
        [2] => ('2. What is foo?')
    )

$arrAnswers output
    Array
    (
        [1] => Array
            (
                [0] => ('a. foo1', 1, 0)
                [1] => ('b. foo2', 1, 0)
                [2] => ('c. foo3', 1, 0)
                [3] => ('d. foo4', 1, 0)
            )

        [2] => Array
            (
                [0] => ('a. foo1', 2, 0)
                [1] => ('b. foo2', 2, 0)
                [2] => ('c. foo3', 2, 0)
                [3] => ('d. foo4', 2, 0)
            )

    )

我的插入记录代码:

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$strDbQuery = "INSERT INTO `question` (`q_name`) VALUES ". implode(", ", $arrQuestions);
$strDbQuery_2 = "INSERT INTO `answers` (`choices`, `question`, `correct`) VALUES ". implode(", ", $arrAnswers);
// use exec() because no results are returned
$conn->exec($strDbQuery);
$conn->exec($strDbQuery_2);
echo "New questions uploaded successfully";
}
catch(PDOException $e)
{
echo $strDbQuery . "<br>" . $e->getMessage();
echo $strDbQuery_2 . "<br>" . $e->getMessage();
}

$conn = null;

我打印了交易的消息,并在插入答案时,插入的值是一个数组。我使用了函数implode(),但它仍然被视为一个数组。

INSERT INTO `question` (`q_name`) VALUES ('1. What is foo1?'), ('2. What is foo2?')  
INSERT INTO `answers` (`choices`, `question`, `correct`) VALUES Array, Array

我该怎么办?

【问题讨论】:

  • 使用 print_r($arrAnswers); 的输出进行编辑
  • @AdrianCidAlmaguer 我更新了我的问题,如果我的输出不清楚,非常抱歉,感谢您指出!
  • implode() 不适用于多维数组。你可以试试这个解决方案:stackoverflow.com/questions/16710800/…
  • @rodrigovr 感谢您的帮助,这提供了丰富的信息。

标签: php mysql sql arrays pdo


【解决方案1】:

编辑:更通用

$strDbQuery_2 = "INSERT INTO `answers` (`choices`, `question`, `correct`) VALUES ";

for($i = 1; $i <= count($arrAnswers); $i++) {
    $strDbQuery_2 .= implode(", ", $arrAnswers[$i]) . ', ';
}

$strDbQuery_2 = substr($strDbQuery_2, 0, -2) . ';';

【讨论】:

  • 这真的很有帮助而且很彻底!
猜你喜欢
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 2019-06-26
  • 2012-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多