我有一个日期数组,类似下面的
Array
(
[0] => stdClass Object
(
[question_id] => 64
[title] => Question1
[question_type_id] => 1
[settings] =>
[poll_id] => 5
[answer] => Array
(
[0] => stdClass Object
(
[answer_id] => 1
[poll_id] => 5
[question_id] => 64
[answer] => Answer1-1
[created_at] => 2018-07-08 17:36:15
)
[1] => stdClass Object
(
[answer_id] => 5
[poll_id] => 5
[question_id] => 64
[answer] => Answer1-2
[created_at] => 2018-07-08 19:27:33
)
)
)
[1] => stdClass Object
(
[question_id] => 65
[title] => Question2
[question_type_id] => 6
[settings] => ["više od 2km","manje od 2km"]
[poll_id] => 5
[answer] => Array
(
[0] => stdClass Object
(
[answer_id] => 2
[poll_id] => 5
[question_id] => 65
[answer] => Answer2-1
[created_at] => 2018-07-08 17:36:47
)
[1] => stdClass Object
(
[answer_id] => 6
[poll_id] => 5
[question_id] => 65
[answer] => Answer2-2
[created_at] => 2018-07-09 23:31:31
)
)
)
我需要一个浏览器中的表格才能看起来像 在此处输入图片说明
Question1 Question2 Question3
Answer1-1 Answer2-1 Answer3-1
Answer1-2 Answer2-2 Answer3-2
这是我的代码:
<table class="table">
<thead>
<tr>
<?php foreach ($DATA['question'] as $question): ?>
<th><?php echo htmlspecialchars($question->title); ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<tr>
<?php foreach ($DATA['question'] as $question): ?>
<td><?php for ($i=0,$j=count((array)$question->answer);$i<$j;$i++) {
echo htmlspecialchars($question->answer[$i]->answer);
}?></td>
<?php endforeach; ?>
</tr>
</tbody>
我的问题是我不能在表的下一行中放置Answer1-2。 任何想法? 非常感谢
答案 0 :(得分:0)
有几种方法可以做到这一点。
for(i=0;i<maxAmountOfAnswers;i++)
,其中包含另一个foreach
(问题)并逐行填充表格。考虑一下为什么要这么做;语义上的表旨在显示一种数据。有一个很好的理由为什么您不只显示每个问题1个表或根本不显示表?
答案 1 :(得分:0)
非常感谢您的回答!这是我的解决办法。
<table class="table">
<thead>
<tr>
<?php foreach ($DATA['question'] as $question): ?>
<th><?php echo htmlspecialchars($question->title); ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<tr>
<?php foreach ($DATA['question'] as $question): ?>
<td><?php for ($i=0,$j=count((array)$question->answer);$i<$j;$i++) {
echo htmlspecialchars($question->answer[$i]->answer);
echo "<br />";
}?></td>
<?php endforeach; ?>
</tr>
</tbody>
</table>
那是什么样子。