簡體   English   中英

輸出多維數組,僅顯示單選按鈕(嵌套foreach循環)

[英]Output multidimensional array, only radio buttons showing (nested foreach loop)

因此,我試圖為一個簡單的測驗輸出一個多維數組。 我認為這是一個邏輯錯誤,但我對此抓狂了。 問題和答案選擇不會顯示在瀏覽器中,但是所有單選按鈕都會顯示。

任何幫助/提示將不勝感激。

<body>
<?php
$q_and_ans = array(
    array('question' => 'What\'s the narrators name?',
        'ans' => array(
            'Greg Scmitz',
            'Greg Sanders',
            'Greg Smith'),
            'correct_ans' => 0),
array('question' => 'What is Greg\'s new friend\'s name?',
    'ans' => array(
        'Jose',
        'Man`enter code here`uel',
        'Manuelo'),
        'correct_ans' => 1),
array('question' => 'What beer are they drinking?',
    'ans' => array(
        'Pacifico',
        'Corona',
        'Pilsener'),
        'correct_ans' => 2),
array('question' => 'Where was the Mariscopa video taken?',
    'ans' => array(
        'Ecuador',
        'El Salvador',
        'Mexico'),
        'correct_ans' => 1),
array('question' => 'What was in the narrator\'s bowl that he failed to mention?',
    'ans' => array(
        'Shrimp',
        'Octopus',
        'Squid'),
        'correct_ans' => 0),
array('question' => 'What color was Manuel\'s shirt?',
    'ans' => array(
        'Green',
        'White',
        'Blue'),
        'correct_ans' => 2),
array('question' => 'What was printed on the beer bucket?',
    'ans' => array(
        'Corona Light',
        'Corona Extra',
        'Modelo'),
        'correct_ans' => 1),
array('question' => 'What kind of vehicle is behind Manuel?',
    'ans' => array(
        'Car',
        'Truck',
        'There is no vehicle'),
        'correct_ans' => 1),
array('question' => 'What color is the roof over the narrator\s head?',
    'ans' => array(
        'Brown',
        'Tan',
        'Green'),
        'correct_ans' => 2),
array('question' => 'What kind of animal is on Manuel\s shirt?',
    'ans' => array(
        'Aligator',
        'Duck',
        'There is no animal'),
        'correct_ans' => 0),

)
?>

我認為問題出在嵌套的foreach循環中

<form method="post" action="">

    <?php 
        foreach($q_and_ans as $i => $q_and_an):?>

            <p><?php $q_and_an['question']?></p>
            <?php foreach($q_and_an['ans'] as $a  => $ans): ?>
            <input type="radio" value="$a" name="question[$i]"> <?php $ans ?>

            <?php endforeach;?>
    <?php endforeach;?>

    <input type="submit" name="submit" id="submit">
</form> 

“ value =” $ a“ name =” question [$ i]“ $ a和$ i不在php標記中。我會使用<?php echo $q_and_an['question'];?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM