好的,所以我已经四处寻找,我似乎无法得到答案,我有两个阵列,一个用于问题,一个用于答案,我试图用嵌套列出每个问题及其答案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; ?>
编辑:
这里是数组的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
)
感谢任何帮助!
答案 0 :(得分:1)
你可以使用这样的东西
<?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 />";
}
?>
答案 1 :(得分:0)
你的数组结构应该是这样的
<?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
}
?>
我认为这会对你有帮助