2014-12-08 63 views
0

好的,所以我已經搜索了四周,我似乎無法得到答案,我兩個數組,一個問題和一個答案,我試圖列出每個問題和他們的答案使用嵌套的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 
) 

任何幫助表示讚賞!

+0

告訴我們您的陣列結構 – manuyd 2014-12-08 05:29:59

+0

需要知道你的兩個數組的結構。 – 2014-12-08 05:32:15

+0

我剛剛添加了印刷的陣列 – 2014-12-08 05:33:58

回答

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 
       } 

      ?> 

我認爲這將有助於你

+0

所以,你說我需要安排結構以便它的功能? – 2014-12-08 06:53:23

+0

是的,我認爲那會更好。 – 2014-12-08 08:27:09

+0

所以這對你有幫助嗎? – 2014-12-10 06:31:39

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 />"; 
} 
?> 
相關問題