如何添加<;tr>;并完成</tr>;PHP foreach循环中的标记
我在如何添加<;tr>;并完成</tr>;PHP foreach循环中的标记,php,arrays,html-table,Php,Arrays,Html Table,我在$form\u all\u data变量中有这种数组 Array ( [0] => stdClass Object ( [id] => 111 [form_id] => 1 [entry_id] => 38 [meta_key] => 6 [meta_value] => Student 1
$form\u all\u data
变量中有这种数组
Array
(
[0] => stdClass Object
(
[id] => 111
[form_id] => 1
[entry_id] => 38
[meta_key] => 6
[meta_value] => Student 1
[item_index] =>
)
[1] => stdClass Object
(
[id] => 112
[form_id] => 1
[entry_id] => 38
[meta_key] => 3
[meta_value] => Not sure
[item_index] =>
)
[2] => stdClass Object
(
[id] => 113
[form_id] => 1
[entry_id] => 38
[meta_key] => 5
[meta_value] => 55
[item_index] =>
)
[3] => stdClass Object
(
[id] => 129
[form_id] => 1
[entry_id] => 43
[meta_key] => 6
[meta_value] => Student 3
[item_index] =>
)
[4] => stdClass Object
(
[id] => 130
[form_id] => 1
[entry_id] => 43
[meta_key] => 3
[meta_value] => Yes
[item_index] =>
)
[5] => stdClass Object
(
[id] => 131
[form_id] => 1
[entry_id] => 43
[meta_key] => 5
[meta_value] => 55
[item_index] =>
)
)
我正在
中显示所有这些数据。这些条目根据条目id
进行区分。
我想在每次新的条目id
出现在循环中时添加
标记,并想在每次相同的条目id
最后出现在循环中时完成
标记
像这样的。请检查这张图片
这意味着我想在一个
中显示条目id
<38数据,在另一个中显示条目id
<43数据
我该怎么做
在我的代码中,它是在每次新的迭代中添加
<?php
$i = 1;
foreach ( $form_all_data as $check_form_all_data_key => $check_form_all_data_value ) { ?>
<tr>
<?php
if ( $check_form_all_data_value->form_id == 1 ) {
if ( $check_form_all_data_value->meta_key == 6 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
if ( $check_form_all_data_value->meta_key == 3 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
} else {
} ?>
</tr>
<?php $i++; } ?>
$rows=[];
foreach($form_all_data as$row){
如果(!isset($rows[$row->id])){
$rows[$row->id]=[];
}
$rows[$row->id][]=$row;
}
$html='';
foreach($行作为$行){
$html.='';
foreach($行作为$单元格){
$html.=''.$cell->meta_值';
}
$html.='';
}
我认为应该使用switch case作为form\u id,if子句作为meta\u key。例如:
<?php
$row = "";
foreach ($form_all_data as $check_form_all_data_key => $check_form_all_data_value)
{
$row .= "<tr>";
switch ($check_form_all_data_key->form_id)
{
case 1:
if ($check_form_all_data_value->meta_key == 6 || $check_form_all_data_value > meta_key = 3)
$row .="<td>".$check_form_all_data_value->meta_value."</td>";
break;
case 2:
if ($check_form_all_data_value->meta_key == 1 || $check_form_all_data_value > meta_key = 5 || $check_form_all_data_value > meta_key = 2)
$row .="<td>".$check_form_all_data_value->meta_value."</td>";
break;
default:
// more case form_id you want check
break;
}
$row .= "</tr>";
}
echo $row;
?>
这是工作代码
<?php
$form_all_data = [
(object)['entry_id' => 1, 'value' => '1 1', 'type' => '1'],
(object)['entry_id' => 1, 'value' => '1 2', 'type' => '2'],
(object)['entry_id' => 2, 'value' => '2 1', 'type' => '1'],
(object)['entry_id' => 2, 'value' => '2 2', 'type' => '2'],
];
$currentEntryId = null;
$needCloseTr = false;
echo '<table>';
foreach ($form_all_data as $row) {
//is next entry?
if ($currentEntryId !== $row->entry_id) {
//close previous tr
if ($needCloseTr) {
echo '</tr>';
$needCloseTr = false;
} else {
$needCloseTr = true;
}
$currentEntryId = $row->entry_id;
echo '<tr>';
}
echo sprintf('<td>%s</td>', $row->value);
}
//close last tr
if ($needCloseTr) {
echo '</tr>';
}
echo '</table>';
这里是@Kevin建议的工作解决方案
<?php
$new_data = array();
foreach ( $form_all_data as $k=> $v ) {
$new_data[$v->entry_id][] = $v;
}
foreach ( $new_data as $new_data_key => $new_data_value ) { ?>
<tr>
<?php
foreach ( $new_data_value as $check_form_all_data_key => $check_form_all_data_value ) {
if ( $check_form_all_data_value->form_id == 1 ) {
if ( $check_form_all_data_value->meta_key == 6 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
if ( $check_form_all_data_value->meta_key == 3 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
} else {
} ?>
<?php } ?>
</tr>
<?php } ?>
检测条目id更改,代码如下
<?php
$i = 1;
$prev_entry_id = 0;
foreach ( $form_all_data as $check_form_all_data_key => $check_form_all_data_value ) { ?>
<?php if($prev_entry_id == 0){
echo "<tr>";
}else if($prev_entry_id != $check_form_all_data_value->entry_id){
echo "</tr><tr>";
} ?>
<?php
if ( $check_form_all_data_value->form_id == 1 ) {
if ( $check_form_all_data_value->meta_key == 6 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
if ( $check_form_all_data_value->meta_key == 3 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
} else {
} ?>
<? $prev_entry_id = $check_form_all_data_value->entry_id; ?>
<?php $i++; } ?>
</tr> // Don't forget this at the end
//最后别忘了这一点
是什么阻止您更改循环?我不知道如何有条件地添加/完成
。您不需要任何类型的条件,只需要收集适当的数据即可。首先按entry\u id
将所有内容分组,然后按行格式排列。然后您可以相应地回显每个
。要对它们进行分组,通常使用的键数组组赋值foreach($k=>v形式的所有数据){$new\u数据[$v['entry\u id']]=$v}
Thank Your@Kevin您的解决方案有效,我没有意识到这么简单。我刚刚编辑了位:$new_data=array();foreach($k=>$v形式的所有数据){$new\u数据[$v->entry\u id][]=$v;}
请为您的答案添加一些解释,以便其他人可以从中学习。我如何解释更多,我的变量名显示它。什么是“it”?你可以先强调你所做的改变,以及为什么这些改变对解决问题很重要。请在你的答案中添加一些解释,以便其他人可以从中学习
<?php
$new_data = array();
foreach ( $form_all_data as $k=> $v ) {
$new_data[$v->entry_id][] = $v;
}
foreach ( $new_data as $new_data_key => $new_data_value ) { ?>
<tr>
<?php
foreach ( $new_data_value as $check_form_all_data_key => $check_form_all_data_value ) {
if ( $check_form_all_data_value->form_id == 1 ) {
if ( $check_form_all_data_value->meta_key == 6 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
if ( $check_form_all_data_value->meta_key == 3 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
} else {
} ?>
<?php } ?>
</tr>
<?php } ?>
<?php
$i = 1;
$prev_entry_id = 0;
foreach ( $form_all_data as $check_form_all_data_key => $check_form_all_data_value ) { ?>
<?php if($prev_entry_id == 0){
echo "<tr>";
}else if($prev_entry_id != $check_form_all_data_value->entry_id){
echo "</tr><tr>";
} ?>
<?php
if ( $check_form_all_data_value->form_id == 1 ) {
if ( $check_form_all_data_value->meta_key == 6 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
if ( $check_form_all_data_value->meta_key == 3 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
} else {
} ?>
<? $prev_entry_id = $check_form_all_data_value->entry_id; ?>
<?php $i++; } ?>
</tr> // Don't forget this at the end