好的,所以我已經搜索了四周,我似乎無法得到答案,我兩個數組,一個問題和一個答案,我試圖列出每個問題和他們的答案使用嵌套的foreach,它正確列出的問題,但我得到的第一個問題,所有的問題的答案,這裏是我的代碼:foreach中的兩個數組迭代在php中
<?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
)
任何幫助表示讚賞!
告訴我們您的陣列結構 – manuyd 2014-12-08 05:29:59
需要知道你的兩個數組的結構。 – 2014-12-08 05:32:15
我剛剛添加了印刷的陣列 – 2014-12-08 05:33:58