输出多维数组,只显示单选按钮(嵌套的foreach循环)

时间:2017-02-21 21:12:11

标签: php

所以我正在尝试输出一个简单测验的多维数组。我认为这是一个逻辑错误,但我在这个问题上摸不着头脑。问题和答案选项不会显示在浏览器中,但所有单选按钮都会显示。

任何帮助/提示都将不胜感激。

<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> 

1 个答案:

答案 0 :(得分:0)

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