【问题标题】:Multiple iterators in phpphp中的多个迭代器
【发布时间】:2015-07-07 02:55:37
【问题描述】:

我有以下代码。基本上,对于每个问题,如果答案的“question_id”与问题的 id 匹配,我需要它来列出该问题的所有可能答案。它对第一个问题执行此操作,但随后退出循环。

            <?php foreach ($questions as $question) : ?>
            <tr>
                <td><?php echo $question['question']; ?></td>
                <td><?php echo $question['id']; ?></td>
            </tr>
                <?php foreach ($answers as $answer) : ?>
                <?php if ($answer['question_id'] == $question['id']) { ?>
                <tr>
                    <td>&nbsp;&nbsp;&nbsp;<?php echo $answer['answer']; ?></td>
                </tr>
                <?php } ?>
                <?php endforeach ?>
            <?php endforeach; ?>
            </tr>

如何继续遍历 $question['id'] 的其余值?

【问题讨论】:

  • 你能补充一下$questions里面的内容吗?
  • 它是一个问题对象数组,有id、question(实际的字符串问题)和survey_id,即所属调查的id。 $answers 是答案对象数组(id、answer、question_id)。
  • 对我来说看起来不错,会为每个数组添加一个示例来测试它
  • 逻辑上看起来不错。您可能没有像预期的那样在变量中获得正确的数据。对 $questions 和 $answers 运行 print_r,看看您是否看到它们包含您期望的所有数据。

标签: php iteration


【解决方案1】:

这基本上是因为您缺少结束分号

<?php endforeach ?> // <-- here

如果你正确设置了error reporting,那应该会给你一个致命错误。

【讨论】:

    猜你喜欢
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2013-02-14
    • 2021-11-21
    • 1970-01-01
    • 2012-02-07
    相关资源
    最近更新 更多