Php 无法解析多数组
我试图解析这个数组以便将它保存在数据库中,但我做不到 这就是Php 无法解析多数组,php,arrays,codeigniter,Php,Arrays,Codeigniter,我试图解析这个数组以便将它保存在数据库中,但我做不到 这就是$This->input->post(“问题”)产生的结果: Array ( [1] => Array ( [0] => question 1 [answer] => Array ( [0] => answer 1 [1] =>
$This->input->post(“问题”)
产生的结果:
Array
(
[1] => Array
(
[0] => question 1
[answer] => Array
(
[0] => answer 1
[1] => answer 2
[2] => answer 3
[3] => asnwer 4
[4] => answeer 5
)
)
[2] => Array
(
[0] => queston 2
[answer] => Array
(
[0] => answer 21
[1] => answere 22
[2] => anwer 23
[3] => answer 24
[4] => answer 25
)
)
)
我试过这个:
foreach ($this->input->post("question") as $questions) {
foreach ($questions as $question) {
$data = array(
'question' => $question,
);
$this->db->insert('questions', $data);
$question_id = $this->db->insert_id();
//another foreach to go throw answers
}
}
只是为了检查我是否正确保存了问题,但我收到了以下消息:
消息:数组到字符串转换
首先,您得到该消息是因为您正在尝试将数组转换为字符串 第二:
foreach ($this->input->post("question") as $questions) {
}
第一级。在此之后,您将继续:
[1] => Array
(
[0] => question 1
[answer] => Array
(
[0] => answer 1
[1] => answer 2
[2] => answer 3
[3] => asnwer 4
[4] => answeer 5
)
)
foreach ($this->input->post("question") as $questions) {
foreach ($questions as $question) {
//here, $question is formed of 2 arrays
//[0] => question 1
//[answer] => Array
// (
// [0] => answer 1
// [1] => answer 2
// [2] => answer 3
// [3] => asnwer 4
// [4] => answeer 5
// )
}
}
这是第二个层次。因此,在该foreach中,您应该使用:
$data = array(
'question' => $question[0], // to get the question string
);
如果还需要什么,请告诉我
p.S:要想找到答案,你必须更深一层,再做一个foreach。在这个数组中谁是谁?谁是
$this->input->post(“问题”)
?@arestraguna$this->input->post(“问题”)生成打印的数组当谁是$exam\u id
?@arestraguna不在乎它,我更新了我的代码