[英]foreach two arrays iteration in php
Ok, so I already searched around and I can't seem to get an answer, I two arrays, one for questions and one for answers, I'm trying to list each question and their answers using nested foreach, it does list the questions correctly but i get the answers of the first question to all of the questions, here's my code: 好的,所以我已经搜索了一下,似乎无法找到答案,我有两个数组,一个数组用于问题,一个数组用于答案,我试图使用嵌套的foreach列出每个问题及其答案,它确实列出了问题正确,但是我得到所有问题的第一个问题的答案,这是我的代码:
<?php foreach($questions as $question): ?>
<p style="font-weight:bold;"><?=$id?> <?=$question['question']?></p>
<?php if($question['type'] == 1): ?>
<?php foreach($answers as $id => $answer):?>
<input type="radio" name="<?=$answer['ans_id']?>" value="<?=$answer['points']?>"> <?=$answer['answers']?><br />
<?php endforeach; ?>
<br />
<?php endif; ?>
<hr>
<?php $id++; ?>
<?php endforeach; ?>
Edit: 编辑:
Here's a print_r of the arrays: 这是数组的print_r:
Array
(
[id] => 1
[quiz_id] => 1
[type] => 1
[question] => ¿Te enojas facilmente?
)
Array
(
[ans_id] => 1
[question_id] => 1
[quiz_id] => 1
[answers] => Si, soy impulsivo y explosivo
[points] => 1
)
Array
(
[ans_id] => 2
[question_id] => 1
[quiz_id] => 1
[answers] => No, soy bien pacifico
[points] => 5
)
Array
(
[ans_id] => 3
[question_id] => 1
[quiz_id] => 1
[answers] => No, soy bien pacifico
[points] => 5
)
Array
(
[id] => 2
[quiz_id] => 1
[type] => 1
[question] => ¿Cuantos amigos tienes?
)
Array
(
[ans_id] => 1
[question_id] => 1
[quiz_id] => 1
[answers] => Si, soy impulsivo y explosivo
[points] => 1
)
Array
(
[ans_id] => 2
[question_id] => 1
[quiz_id] => 1
[answers] => No, soy bien pacifico
[points] => 5
)
Array
(
[ans_id] => 3
[question_id] => 1
[quiz_id] => 1
[answers] => No, soy bien pacifico
[points] => 5
)
Array
(
[id] => 3
[quiz_id] => 1
[type] => 1
[question] => ¿cuantas veces?
)
Array
(
[ans_id] => 1
[question_id] => 1
[quiz_id] => 1
[answers] => Si, soy impulsivo y explosivo
[points] => 1
)
Array
(
[ans_id] => 2
[question_id] => 1
[quiz_id] => 1
[answers] => No, soy bien pacifico
[points] => 5
)
Array
(
[ans_id] => 3
[question_id] => 1
[quiz_id] => 1
[answers] => No, soy bien pacifico
[points] => 5
)
Any help is appreciated! 任何帮助表示赞赏!
You can use something like this 你可以用这样的东西
<?php
$First = array('a', 'b', 'c', 'd');
$Second = array('1', '2', '3', '4');
for ($indx = 0 ; $indx < count($First); $indx ++) {
echo $First[$indx] . $Second[$indx];
echo "<br />";
}
?>
Your array structure should be like this 你的数组结构应该是这样的
<?php
$question = array(
'qid1' => 'question 1?'
'qid2' => 'question 2?'
//...
);
$answer = array(
'qid1' => array(
'ansid-1' => 'answer for q 1 1'
'ansid-2' => 'answer for q 1 2'
'ansid-3' => 'answer for q 1 3'
'ansid-4' => 'answer for q 1 4'
),
'qid2' => array(
'ansid-5' => 'answer for q 2 1'
'ansid-6' => 'answer for q 2 2'
'ansid-7' => 'answer for q 2 3'
'ansid-8' => 'answer for q 2 4'
)
)
foreach( $question $qid=>$q){
// + question related html start
foreach($answer[$qid] as $answer_id => $answer){
// + answer related html
}
// + question related html end
}
?>
I think this will help you 我想这对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.